Closed Bug 1266654 Opened 10 years ago Closed 10 years ago

document.execCommand('copy') is unavailable for context menu command

Categories

(WebExtensions :: Untriaged, defect)

defect
Not set
normal

Tracking

(firefox48 affected)

RESOLVED DUPLICATE of bug 1197451
Tracking Status
firefox48 --- affected

People

(Reporter: yuki, Unassigned)

Details

Attachments

(1 file)

To implement a feature to copy generated text to the clipboard, document.execCommand('copy') in the background page is only one known method for WebExtensions-based addons. (It is a known standard method for Chrome extensions: http://stackoverflow.com/questions/27892282/copy-selected-text-via-a-context-menu-option-in-a-chrome-extension ) However, document.execCommand('copy') always fails if it is triggered from a context menu item added by the addon itself. Steps to reproduce: 1) Define a function using "document.execCommand('copy')". 2) Define a context menu item using the function defined at 1). 3) Go to "about:debugging". 4) Click the "Load Temporary Add-on" button and load the addon. 5) Open the browser console by Ctrl-Shift-J. 6) Open the context menu on a webpage and choose the menu item defined at 2). Actual result: A warning "document.execCommand('cut'/'copy') was denied because it was not called from inside a short running user-generated event handler." appears in the browser console and nothing is copied to the clipboard. Expected result: Something is copied to the clipboard. The attached addon is a minimum example. It includes only one background script: --------------------------------------------------- function setClipBoard(aString) { var container = document.createElement('textarea'); document.documentElement.appendChild(container); container.value = aString; container.select(); document.execCommand('copy'); container.parentNode.removeChild(container); } chrome.contextMenus.create({ type : 'normal', id : 'context-copy-date', title : 'Copy current date by addon', onclick : function(aInfo, aTab) { setClipBoard(String(new Date())); } }); --------------------------------------------------- The code adds a new context menu item "Copy current date by addon". After installation, go to any webpage, open the context menu in the content area, and choose the added item. Then you'll see the "Actual result".
The message is from here: http://mxr.mozilla.org/mozilla-central/source/dom/html/nsHTMLDocument.cpp#3244 --------------------------------------------------- 3244 // special case for cut & copy 3245 // cut & copy are allowed in non editable documents 3246 if (isCutCopy) { 3247 if (!nsContentUtils::IsCutCopyAllowed()) { 3248 // We have rejected the event due to it not being performed in an 3249 // input-driven context therefore, we report the error to the console. 3250 nsContentUtils::ReportToConsole(nsIScriptError::warningFlag, 3251 NS_LITERAL_CSTRING("DOM"), this, 3252 nsContentUtils::eDOM_PROPERTIES, 3253 "ExecCommandCutCopyDeniedNotInputDriven"); 3254 return false; 3255 } --------------------------------------------------- "nsContentUtils::IsCutCopyAllowed()" seems to return "true" if the code is triggered from any user event or the script has the chrome privilege. I think the background script is also treated as a script with chrome privilege. I'm planning to implement a command to copy generated text with delay - because it requires inter-sandboxes communication to collect information from another content script. Even if context menu's "click" event is treated as an user event, the planned command will fail because deferred operations are processed after the "click" event finished.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → DUPLICATE
Product: Toolkit → WebExtensions
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: