我最近第一次创建了一个chrome扩展 . 我的页面上有一个复制按钮,当扩展名作为网页加载时可以正常工作 . 但是,当我将其作为扩展名加载时,复制功能不起作用 .
我的 manifest.json
权限如下所示:
"permissions": [
"webNavigation",
"storage",
"clipboardWrite",
"clipboardRead"
]
要复制的 popout.html
页面中的代码如下所示:
<div id="legacyCopyLabel">
<a onclick="copyText(getElementById('legacy'))" class="alignright">COPY</a>
</div>
我的javascript代码中的 copyText()
函数如下所示:
function copyText(element) {
var range, selection, worked;
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(element);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
try {
document.execCommand('copy');
updateStatus ('Copied address', 'info');
}
catch (err) {
alert('Unable to copy the address');
}
}
如果您想要查看完整代码:https://github.com/markallisongit/handcash-chrome
Chrome扩展程序位于Chrome商店:https://chrome.google.com/webstore/detail/handcash-handle-converter/bnkohbkbipagdhplhkhgonbalbjigpoh
1 回答
它不起作用,因为你使用内联JS:
<a onclick="...">
.Chrome的内容安全警察称:
解决方案是将您的代码放入一个单独的
.js
文件中,并将其作为常用的JS脚本包含在内 .