0

Testpage: https://www.google.com

It works in Chrome but in Firefox Nightly 52.0a1 it gives me this error when clicked on the notification:

document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler

copyTextToClipboard() function was taken from 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>"]
    }
);
Community
  • 1
  • 1
Vega
  • 2,661
  • 5
  • 24
  • 49
  • The documentation is pretty clear on this, *"In Firefox 41 and later, clipboard capability are enabled by default in any event handler* ***that is able to pop-up a window (semi-trusted scripts)*** *."*, I don't think there's any way around that, other than using Flash etc. or plugins that makes using Flash easier, like ZeroClipBoard. – adeneo Oct 02 '16 at 20:19
  • So if I would add a pop-up window in the onClicked.addListener() function it could work? – Vega Oct 02 '16 at 20:27
  • https://bugzil.la/1197451 – Daniel Herr Oct 02 '16 at 20:51
  • Well, no, the point wasn't to open a popup, the point was that any function that is allowed to open a new window, would also be allowed to use the clipboard, and that would be functions that are directly initiated by user action. – adeneo Oct 02 '16 at 20:58
  • At a minimum, you are going to need the `clipboardWrite` permission in *manifest.json* (see bug linked by Daniel Herr, and that you mentioned in a [comment on your prior question](http://stackoverflow.com/questions/39773656/chrome-notification-create-with-chrome-notification-onclicked-in-a-firefox-webex/39780548?noredirect=1#comment66930587_39780548)). Adding that permission does not make it actually work. It does, however, eliminate the console error, so now it just fails silently. – Makyen Oct 02 '16 at 20:59
  • Note: It is a good idea to test your code in Chrome prior to saying that it works in Chrome. Your code does not provide all the options required by Chrome (different ones are required by Chrome than Firefox, e.g. `iconUrl`) to `chrome.notifications.create`. Thus, the notification is never created. – Makyen Oct 02 '16 at 21:02
  • You're right, mixed up the example extension with my real one. Editet the iconUrl in. – Vega Oct 02 '16 at 21:12
  • After adding a `iconUrl` it does work in Chrome. Note to test in Firefox (using Windows 10 x64, tested Firefox Developer Edition, and Nightly) it is necessary to rate limit the creation of the notifications, otherwise either a notification briefly flashes on the screen and disappears, or is not displayed at all. For others reading this, for more detail see [this answer](http://stackoverflow.com/a/39780548/3773011). It would be better to have a [mcve] which uses a `browser_action` to display the notification now that the example is not dependent on the URL from the `onBeforeRequest`. – Makyen Oct 02 '16 at 21:16

1 Answers1

0

See 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

Sebastian Blask
  • 2,870
  • 1
  • 16
  • 29