creator 有复制内容的到剪切版的API么
没有,需要分平台用平台 API 实现
啥意思?不是特别明白?
意思就是你要在哪个平台上使用就用那个平台的api,cocos不提供这样的借口
你的意思要是复制到QQ里面就用QQ的API?那如果我要粘贴到任何地方呢,就把内容添加到剪切板里,到哪粘贴都可以
比如我要把editBox里面的内容复制到剪切板,能用JS代码实现么
creator的工具功能比较薄弱,这个自己去实现吧
不会啊,JS如何把一个字符串,塞到剪切板呀,网上找了都不能用
每个平台做法是不一样的
行吧,表示不会整
Android上用ClipboardManager这个类,其他平台的不清楚
editBox 里面的文字可以用JS强行选中么
/**
- Copy the String representation of a value to the clipboard.
- @param any aValue
-
A value you want to copy as a string. -
@return void
/
WebConsoleCommands._registerOriginal(“copy”, function JSTH_copy(aOwner, aValue)
{
let payload;
try {
if (aValue instanceof Ci.nsIDOMElement) {
payload = aValue.outerHTML;
} else if (typeof aValue == “string”) {
payload = aValue;
} else {
payload = JSON.stringify(aValue, null, " ");
}
} catch (ex) {
payload = "/ " + ex + " */";
}
aOwner.helperResult = {
type: “copyValueToClipboard”,
value: payload,
};
});
web平台可以考虑使用这个
https://github.com/sindresorhus/clipboardy
使用起来也很方便,将一下代码新建为copy-text-to-clipboard.js
'use strict';
module.exports = input => {
const el = document.createElement('textarea');
el.value = input;
// Prevent keyboard from showing on mobile
el.setAttribute('readonly', '');
el.style.contain = 'strict';
el.style.position = 'absolute';
el.style.left = '-9999px';
el.style.fontSize = '12pt'; // Prevent zooming on iOS
const selection = getSelection();
let originalRange = false;
if (selection.rangeCount > 0) {
originalRange = selection.getRangeAt(0);
}
document.body.appendChild(el);
el.select();
// Explicit selection workaround for iOS
el.selectionStart = 0;
el.selectionEnd = input.length;
let success = false;
try {
success = document.execCommand('copy');
} catch (err) {}
document.body.removeChild(el);
if (originalRange) {
selection.removeAllRanges();
selection.addRange(originalRange);
}
return success;
};
代码中使用
let copy = require("copy-text-to-clipboard");
let b = copy("剪切内容");
if(b){
console.log("复制成功!");
}
2赞
getSelection is not defined
在微信小程序上不能使用
el.select() 找不到此方法,在微信小游戏和百度小游戏中无效
微信小游戏就用微信小游戏提供的借口