html5Plus 读写剪切板(粘贴板读取、写入、编辑)
空了封装个Helper类,先将就看吧。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<style>
button {
width: 66px;
height: 88px;
}
body {
background-color: blueviolet;
}
</style>
</head>
<body>
<button id="writeClipBoardButton">写入剪切板</button>
<button id="readClipBoardButton">读取剪切板</button>
<script type="text/javascript">
//测试写入剪切板
testWriteClipBoardButton();
//测试读取剪切板
testReadClipBoardButton()
//测试写入剪切板
function testWriteClipBoardButton()
{
//写入剪切板按钮
var writeClipBoardButton = document.getElementById('writeClipBoardButton');
writeClipBoardButton.onclick = function()
{
//检测plus功能是否已经可以使用
if(window.plus)
{
//写入的内容
var writeText = "Panda666" + Math.random();
//写入剪切板
writeTextToClipBoard(writeText);
}
else
{
console.log("Plus 功能未启用");
}
};
}
//测试读取剪切板
function testReadClipBoardButton()
{
//读取剪切板按钮
var readClipBoardButton = document.getElementById('readClipBoardButton');
readClipBoardButton.onclick = function()
{
//检测plus功能是否已经可以使用
if(window.plus)
{
//读取剪切板
var clipBoardText = readTextFormClipBoard();
console.log(clipBoardText);
}
else
{
console.log("Plus 功能未启用");
}
};
}
/**
* 复制文本到剪切板
* @param {string} text
*/
function writeTextToClipBoard(text)
{
if (plus.os.name == "Android")
{
var Context = plus.android.importClass("android.content.Context");
var main = plus.android.runtimeMainActivity();
var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
plus.android.invoke(clip, "setText", text);
}
else if (plus.os.name == "iOS")
{
var UIPasteboard = plus.ios.importClass("UIPasteboard");
var generalPasteboard = UIPasteboard.generalPasteboard();
//设置/获取文本内容:
generalPasteboard.plusCallMethod({
setValue: text,
forPasteboardType: "public.utf8-plain-text"
});
generalPasteboard.plusCallMethod({
valueForPasteboardType: "public.utf8-plain-text"
});
}
}
/**
* 读取剪切板的内容
* 注意:IOS端未进行测试
*/
function readTextFormClipBoard()
{
if (plus.os.name == "Android")
{
var Context = plus.android.importClass("android.content.Context");
var main = plus.android.runtimeMainActivity();
var clip = main.getSystemService(Context.CLIPBOARD_SERVICE);
var result = plus.android.invoke(clip, "getText").toString();
return result;
}
else if (plus.os.name == "iOS") // 注意:IOS端未进行测试
{
var UIPasteboard = plus.ios.importClass("UIPasteboard");
var generalPasteboard = UIPasteboard.generalPasteboard();
var _val=generalPasteboard.plusCallMethod({valueForPasteboardType:"public.utf8-plain-text"});
return _val || '';
}
}
</script>
</body>
</html>
本文来自博客园,作者:重庆熊猫,转载请注明原文链接:https://www.cnblogs.com/cqpanda/p/16936533.html