我最近第一次创建了一个chrome扩展。我的页面上有一个copy按钮,当扩展加载为web页面时,它可以正常工作。但是,当我将其作为扩展加载时,copy函数不起作用。
我的
manifest.json
权限如下:
"permissions": [
"webNavigation",
"storage",
"clipboardWrite",
"clipboardRead"
]
我的代码
popout.html
要复制的页如下所示:
<div id="legacyCopyLabel">
<a onclick="copyText(getElementById('legacy'))" class="alignright">COPY</a>
</div>
以及
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