【发布时间】:2017-07-16 22:47:33
【问题描述】:
在 Chrome 中编写一个小型浏览器扩展程序,将某些特定文本从特定网页复制到剪贴板。 HTML 格式,因此人们可以将其粘贴到 Word、Outlook 等办公程序中。
document.execCommand('copy') 是我使用的命令,它是由document.onkeydown 组合键 (Alt+1) 触发的,它工作正常 - 但只是第一次。如果您再次尝试按下组合键,它不会执行任何操作。
我找到了原因,document.queryCommandEnabled("copy") 第一次返回 true,任何其他尝试返回 false。如果我重新加载页面,它会第一次再次返回 true。此外,如果我在加载页面后在浏览器窗口外单击,然后在浏览器中单击并使用组合键,即使是第一次,也会立即返回 false。
function copy(text) {
var sel = document.createElement("div"); // Creating temporary holder for text to copy
sel.style.opacity = 0; sel.style.position = "absolute"; // These are not really required,
sel.style.pointerEvents = "none"; sel.style.zIndex = -1; // at least for Chrome
sel.innerHTML = text; // Put the text to copy into the temporary holder
document.body.appendChild(sel); // Add the temporary holder to the page
var range = document.createRange(); // All this is required to select the text,
range.selectNode(sel); // since the holder is not an editable text
window.getSelection().addRange(range); // field and must be treated differently.
// document.designMode = 'on'; (Tried this, no effect)
/* Debugging */ alert("Enabled = " + document.queryCommandEnabled("copy") + " Design mode = " + document.designMode);
try {
document.execCommand('copy'); // Copy to clipbopard
}
catch (err) {
alert('Unable to copy');
console.log('Unable to copy'); // Copy failed?!
}
// document.designMode = 'off'; (Tried this, no effect)
document.body.removeChild(sel); // Clean up removing the temporary holder
}
document.onkeydown=function(e){
if(e.altKey && e.which == 49) { // Alt+1
copy(link_variable);
return false;
}
}
有什么想法吗?
添加清单文件:
{
"manifest_version": 2,
"name": "Usage text",
"version": "0.2",
"description": "Whatever",
"content_scripts": [
{
"matches": [
"*://some.specific.site.com/*"
],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"browser_action": {
"name": "Whatever",
"default_icon": "icon.png"
},
"permissions": [
"tabs",
"clipboardWrite"
]
}
更新:
将操作从content脚本转移到background脚本,没有变化。
【问题讨论】:
-
您是否在清单中包含了
clipboardWritepermission? -
谢谢,不,我没有,但是现在按照您的评论尝试了,结果是一样的。添加了(更新的)清单文件。
标签: javascript google-chrome clipboard execcommand