1

I'm looking at writing a small Firefox addin/extension wherein I will use the copy/paste buffer. I tried to test the code I saw on MDN in the Firefox / Tools / Web Developer / Web Console / Javascript tab, in order to get an online 'REPL' evaluator, to no avail.

This is what I tried and the error; https://developer.mozilla.org/en-US/docs/Using_the_Clipboard Components.utils.import('resource://gre/modules/Services.jsm'); TypeError: Components.utils is undefined

I am very new to this and perhaps am misreading the docs, but it seemed like this was the correct page and the correct place to start per other readings, eg Firefox Xul Clipboad thank you!

Community
  • 1
  • 1
AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48
  • Where are you trying to import Services.jsm? You need to show the code to find out the problem. – erosman Jan 02 '15 at 06:53
  • 1
    this topic is good: http://stackoverflow.com/questions/26545871/paste-data-from-clipboard-using-document-execcommandpaste-within-firefox-ex/26554409#26554409 you might find this topic helpful although its just redudant mdn copy paste: http://stackoverflow.com/questions/27034198/interacting-with-firefox-addons-by-javascript/27180064#27180064 | – Noitidart Jan 02 '15 at 11:35
  • 1
    You have to paste this javascript code in privealged scope. Enable dev prefs: https://developer.mozilla.org/en-US/Add-ons/Setting_up_extension_development_environment?redirectlocale=en-US&redirectslug=Setting_up_extension_development_environment#Development_preferences then press shift+f4 to open scratcpad. set the environment menu to browser. then paste code and hit run. this way you will not get the Components.utils is undefined issue – Noitidart Jan 02 '15 at 11:37
  • @Noitidart, thank you! On OSX , the Scratchpad window's menu bar, which is at the top of my screen, has no environment menu, only file edit view execute help. Is there another way to access it? – AnneTheAgile Jan 03 '15 at 22:33
  • but that helped me find it! posted below. – AnneTheAgile Jan 03 '15 at 22:47
  • My pleasure. To get the "Environment" menu to show up on OSX, the method is same as for all other OS, enable those developer preferences. I use OSX. – Noitidart Jan 03 '15 at 23:26
  • @Noitidart, do you enable all the 'Recommended development preferences' per https://developer.mozilla.org/en-US/Add-ons/Setting_up_extension_development_environment?redirectlocale=en-US&redirectslug=Setting_up_extension_development_environment#Development_preferences ? Those omit the webgl.enable-privileged-extensions item I saw in about:config that seems to be what you referenced? Being unsure which config was right is why I poked around a bit more. – AnneTheAgile Jan 04 '15 at 00:01
  • The webgl pref is not relavent to getting environment getting Environment option in menu bar. Honestly i just install the devprefs addon to set the prefs for me: https://addons.mozilla.org/en-US/firefox/addon/devprefs/ – Noitidart Jan 04 '15 at 00:03

1 Answers1

1

The answer is that the MDN instructions to use the Firefox / Tools / Web Developer / Web Console / Javascript tab, are apparently not accurate for my version of Firefox v34.0.5 and OSX v.10.8.5.

Instead, the instructions to get to the right, privileged, Javascript console are as follows.

1.Install the Developer Assistant addon in order to get new menu items for several consoles/editors. https://addons.mozilla.org/en-US/firefox/addon/extension-developer/reviews/

2.Click on Firefox / Tools / ExtensionDeveloper / JavascriptShell. The picture below shows the menu selection on the right and the resultant console window on the left (sorry they look a bit backwards).

Menu-Firefox-Tools-ExtensionDev-JsShell

3.This new Javascript shell appears to be unique in that it is privileged and comes with the Components packaged installed. There are three others also available (Chrome ScratchPad, Javascript Environment, Web Console), but none of them include the Components software.

4.Try out pasting to the clipboard by pasting this code into the new console window: var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper); gClipboardHelper.copyString("Put me on the clipboard, please.")

If you have a clipboard manager with a GUI like PthPasteboard, you'll see it appear immediately in the latest buffer. Otherwise, to see it, just type //, the comment prefix, and then control-v or apple-v to paste it on the next line in the buffer; //Put me on the clipboard, please.

Thank you so much to @ Noitidart, who wrote several posts/comments that gave me the code sample plus pointed out the need for a 'privileged' console.

AnneTheAgile
  • 9,932
  • 6
  • 52
  • 48