如何在 Firefox WebExtension 附加组件中通过 chrome.notification.create 和 chrome.notification.onClicked 复制到剪贴板?
How to copy to clipboard via chrome.notification.create with chrome.notification.onClicked in a Firefox WebExtension add-on?
它在 Chrome 中有效,但在 Firefox Nightly 52.0a1 中单击通知时出现此错误:
document.execCommand(‘cut’/‘copy’) was denied because it was not
called from inside a short running user-generated event handler
copyTextToClipboard() 函数取自 Copy to Clipboard in Chrome Extension
manifest.js
{
"description": "Test for JSON Notifiaction + Clipboard Copy",
"manifest_version": 2,
"name": "Test3",
"version": "1.0",
"permissions": [
"<all_urls>",
"clipboardWrite",
"notifications",
"webRequest"
],
"background": {
"scripts": ["background.js"]
}
}
background.js
'use strict';
let JSON_obj = {
"name" : "ABCDEFG",
"age" : 3,
};
function logURL(requestDetails) {
// filter rules to check requestDetails.url for specific parameters {
notify(JSON_obj);
// }
}
function notify(notifyMessage) {
var options = {
type: "basic",
iconUrl: chrome.extension.getURL("icons/test.png"),
title: "",
message: JSON.stringify(notifyMessage, null, "\t")
};
chrome.notifications.create("uniqueID3", options);
}
chrome.notifications.onClicked.addListener(function() {
console.log('Clicked notification message text: ', JSON_obj);
copyTextToClipboard(JSON.stringify(JSON_obj, null, "\t"));
});
function copyTextToClipboard(copyText) {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = copyText;
var body = document.getElementsByTagName('body')[0];
body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
body.removeChild(copyFrom);
}
chrome.webRequest.onBeforeRequest.addListener(
logURL, {
urls: ["<all_urls>"]
}
);
参见https://github.com/mdn/webextensions-examples/blob/master/context-menu-copy-link-with-types/clipboard-helper.js#L2 - you can't copy from a background script. Your copyTextToClipboard code works fine when injected into a page like done in the example: https://github.com/mdn/webextensions-examples/blob/master/context-menu-copy-link-with-types/background.js#L31
它在 Chrome 中有效,但在 Firefox Nightly 52.0a1 中单击通知时出现此错误:
document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler
copyTextToClipboard() 函数取自 Copy to Clipboard in Chrome Extension
manifest.js
{
"description": "Test for JSON Notifiaction + Clipboard Copy",
"manifest_version": 2,
"name": "Test3",
"version": "1.0",
"permissions": [
"<all_urls>",
"clipboardWrite",
"notifications",
"webRequest"
],
"background": {
"scripts": ["background.js"]
}
}
background.js
'use strict';
let JSON_obj = {
"name" : "ABCDEFG",
"age" : 3,
};
function logURL(requestDetails) {
// filter rules to check requestDetails.url for specific parameters {
notify(JSON_obj);
// }
}
function notify(notifyMessage) {
var options = {
type: "basic",
iconUrl: chrome.extension.getURL("icons/test.png"),
title: "",
message: JSON.stringify(notifyMessage, null, "\t")
};
chrome.notifications.create("uniqueID3", options);
}
chrome.notifications.onClicked.addListener(function() {
console.log('Clicked notification message text: ', JSON_obj);
copyTextToClipboard(JSON.stringify(JSON_obj, null, "\t"));
});
function copyTextToClipboard(copyText) {
var copyFrom = document.createElement("textarea");
copyFrom.textContent = copyText;
var body = document.getElementsByTagName('body')[0];
body.appendChild(copyFrom);
copyFrom.select();
document.execCommand('copy');
body.removeChild(copyFrom);
}
chrome.webRequest.onBeforeRequest.addListener(
logURL, {
urls: ["<all_urls>"]
}
);
参见https://github.com/mdn/webextensions-examples/blob/master/context-menu-copy-link-with-types/clipboard-helper.js#L2 - you can't copy from a background script. Your copyTextToClipboard code works fine when injected into a page like done in the example: https://github.com/mdn/webextensions-examples/blob/master/context-menu-copy-link-with-types/background.js#L31