代码之家  ›  专栏  ›  技术社区  ›  Vega

如何通过chrome.notification复制到剪贴板。使用chrome.notification创建。在Firefox WebExtension插件中单击?

  •  0
  • Vega  · 技术社区  · 9 年前

    测试页面: https://www.google.com

    它在Chrome中有效,但在Firefox Nightly 52.0a1中,单击通知时会出现以下错误:

    文件execCommand(cut/copy)被拒绝,因为它没有 从短期运行的用户生成的事件处理程序内部调用

    copyTextToClipboard()函数取自 Copy to Clipboard in Chrome Extension

    {
        "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"]
        }
    }
    

    背景.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>"]
        }
    );
    
    1 回复  |  直到 9 年前
    推荐文章