0

Testpage:https://www.google.com如何通过chrome.notification.create将chrome.notification.onClick在Firefox WebExtension插件中复制到剪贴板?

它可以在Chrome,但在Firefox每晚52.0a1的通知点击的时候给了我这个错误:

document.execCommand( '一刀切'/ '复制')被拒绝因为它不是从 短时间运行的用户生成的事件处理程序内调用

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>"] 
    } 
); 
+0

文档非常清晰,*“在Firefox 41及更高版本中,在任何事件处理程序中默认启用剪贴板功能* ***这是能够弹出一个窗口(半可信脚本)*** *。“*,我不认为有任何解决方法,除了使用Flash等插件,使Flash更容易,像ZeroClipBoard 。 – adeneo

+0

因此,如果我要在onClicked.addListener()函数中添加一个弹出窗口,它可以工作吗? – Vega

+0

https://bugzil.la/1197451 –

回答