I am using a Content script to manipulate data in the DOM.I have been using document.execCommand('copy'); successfully on a popup page.
我使用内容脚本来操作DOM中的数据。我一直在使用document.execCommand('copy');在弹出页面上成功完成。
I am now searching for a way to make it work on a Content script.I have checked the limitations for content scripts here, but I do not understand if Clipboard control is limited or not.I have also checked answers here - in *, but it seems that most are uncertain and some are from a few years ago so there might have been changes.
我现在正在寻找一种方法使其在内容脚本上工作。我已经检查了内容脚本的限制,但是我不明白剪贴板控件是否有限。我也在这里检查了答案 - 在*中,但是似乎大多数都是不确定的,有些是几年前的,所以可能会有变化。
Even if it is limited, is it possible to have some kind of workaround?
即使它有限,是否可以采取某种解决方法?
Thank you!
I am posting the current script that I have.
我发布了我当前的脚本。
manifest.json
{ "name": "Page action by URL", "version": "1.0", "description": "Прибавка за обработка на данните от НБДН.", "background": { "scripts": ["background.js"], "persistent": false }, "page_action" : { "default_icon" : "icon-19.png", "default_title" : "Приложение за НБД за PHP" }, "permissions" : [ "clipboardWrite", "clipboardRead", "declarativeContent", "activeTab", "tabs", "https://nbd.grao.government.bg/graoappshort/*" ], "icons" : { "48" : "icon-48.png", "128" : "icon-128.png" }, "manifest_version": 2}
background.js
chrome.runtime.onInstalled.addListener(function() { // Replace all rules ... chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { // With a new rule ... chrome.declarativeContent.onPageChanged.addRules([ { conditions: [ new chrome.declarativeContent.PageStateMatcher({ pageUrl: { urlContains: 'nbd.grao.government.bg/graoappshort/' }, }) ], actions: [ new chrome.declarativeContent.ShowPageAction() ] } ]); });});chrome.pageAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript(null, {file: 'page-editor.js'}); chrome.tabs.insertCSS(null, {file: "style-inject.css"});});
and the function inside page-editor.js
以及page-editor.js中的函数
function(){ var copyFrom = document.createElement("textarea"); copyFrom.textContent = PoleIME.value; document.body.appendChild(copyFrom); copyFrom.focus(); document.execCommand('SelectAll'); document.execCommand('Copy'); //document.body.removeChild(copyFrom); }
1 个解决方案
#1
Content scripts cannot use the clipboard at the moment. In the future, once crbug.com/395376 is resolved, then the code as shown in the question will work as intended.
内容脚本此刻无法使用剪贴板。将来,一旦crbug.com/395376得到解决,那么问题中显示的代码将按预期工作。
Until that bug is fixed, you have to send the data to the background page and copy the text from there:
在修复该错误之前,您必须将数据发送到后台页面并从那里复制文本:
// content scriptchrome.runtime.sendMessage({ type: 'copy', text: 'some text to copy'});
Script on background page or event page:
背景页面或活动页面上的脚本:
chrome.runtime.onMessage.addListener(function(message) { if (message && message.type == 'copy') { var input = document.createElement('textarea'); document.body.appendChild(input); input.value = message.text; input.focus(); input.select(); document.execCommand('Copy'); input.remove(); }});
#1
Content scripts cannot use the clipboard at the moment. In the future, once crbug.com/395376 is resolved, then the code as shown in the question will work as intended.
内容脚本此刻无法使用剪贴板。将来,一旦crbug.com/395376得到解决,那么问题中显示的代码将按预期工作。
Until that bug is fixed, you have to send the data to the background page and copy the text from there:
在修复该错误之前,您必须将数据发送到后台页面并从那里复制文本:
// content scriptchrome.runtime.sendMessage({ type: 'copy', text: 'some text to copy'});
Script on background page or event page:
背景页面或活动页面上的脚本:
chrome.runtime.onMessage.addListener(function(message) { if (message && message.type == 'copy') { var input = document.createElement('textarea'); document.body.appendChild(input); input.value = message.text; input.focus(); input.select(); document.execCommand('Copy'); input.remove(); }});