dev*_*v02 4 javascript browser plugins google-chrome google-chrome-extension
任何人都可以告诉如何复制整个页面类似于按Ctrl + A然后将当前选项卡复制到剪贴板.
目前我有这个,但它没有做任何事情虽然扩展已成功添加到chrome:
清单文件
"permissions":
[
"clipboardRead",
"clipboardWrite"
],
// etc
Run Code Online (Sandbox Code Playgroud)
内容脚本
chrome.extension.sendRequest({ text: "text you want to copy" });
Run Code Online (Sandbox Code Playgroud)
背景页面
<html>
<head>
<script type="text/javascript">
chrome.extension.onRequest.addListener(function (msg, sender, sendResponse) {
var textarea = document.getElementById("tmp-clipboard");
// now we put the message in the textarea
textarea.value = msg.text;
// and copy the text from the textarea
textarea.select();
document.execCommand("copy", false, null);
// finally, cleanup / close the connection
sendResponse({});
});
</script>
</head>
<body>
<textarea id="tmp-clipboard"></textarea>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
弹出
<textarea id="tmp-clipboard"></textarea>
<input type="button" id="btn" value="Copy Page">
Run Code Online (Sandbox Code Playgroud)
我不能让这个工作,想知道我在这里失踪了什么.
任何人都可以指导如何模仿Ctrl+ A后跟Ctrl+ C当前选项卡,以便它存储在剪贴板中?
您的代码中存在多个问题
消除这些问题后,您的代码按预期工作.
您的用例示例演示
确保清单具有所有权限和注册
{
"name":"Copy Command",
"description":"http://stackoverflow.com/questions/14171654/chrome-extension-how-to-select-all-text-of-tab-and-copy",
"version":"1",
"manifest_version":2,
"background":{
"page":"background.html"
},
"permissions":
[
"clipboardRead",
"clipboardWrite"
],
"content_scripts":[
{
"matches":["<all_urls>"],
"js":["script.js"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
确保它尊重所有安全性变化
<html>
<head>
<script src="background.js"></script>
</head>
<body>
<textarea id="tmp-clipboard"></textarea>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
为Simulate Ctrl+ A和Ctrl+ 添加了监听器C
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {
//Set Content
document.getElementById("tmp-clipboard").value = msg.text;
//Get Input Element
document.getElementById("tmp-clipboard").select();
//Copy Content
document.execCommand("Copy", false, null);
});
Run Code Online (Sandbox Code Playgroud)
传递要复制的内容
chrome.extension.sendMessage({ text: "text you want to copy" });
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3720 次 |
最近记录: |