23

I am developing an extension that will read HTML elements value, then make API calls to an external website, get the results and display them in newly created HTML elements.

What are the pro/cons of writing it in native chrome extensions vs. userscripts?

Ben Collins
  • 20,538
  • 18
  • 127
  • 187
Avi Meir
  • 980
  • 3
  • 11
  • 26
  • Use a userscript unless you need to: Manipulate browser controls; Manipulate files; Do complex interaction between tabs or windows, or track complex data/states across sessions and domains. What you describe so far can easily be handled by a userscript. – Brock Adams Nov 21 '12 at 07:19

3 Answers3

18

There are a few differences between a native Chrome extension and a user script.

See this wiki section for a list of differences between Greasemonkey scripts and Chrome userscripts.

If you want to take advantage of cross-browser user scripts, try to not use GM_* methods or unsafeWindow.

From a developer's perspective, there is no advantage of preferring either User scripts or Chrome extensions, because any user script can easily be embedded in a Chrome extension.

If you view this topic in the light of deployment, then the differences are significant:

  • One-click install is only available in the Chrome Web Store. Native extensions only.
  • Both user scripts and native extensions can be dragged from the local file browser to the Extensions page to install the extension.
    (User scripts are converted to Chrome extensions; warning: see below)
  • Native user script support will stop working at the end of 2013, because converted user scripts use manifest version 1, which has been deprecated.

Conclusion

I recommend to develop native extensions. You can easily create a Chrome extension from a user script by using the following directory structure:

manifest.json
whatever.user.js

Minimal manifest.json:

{
     "name": "name of extension",
     "version": "1",
     "manifest_version": 2,
     "content_scripts": [{
         "js": ["whatever.user.js"],
         "matches": ["http://example.com/*"]
      }]
}

See also

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Manifest version 1 has been deprecated, but where does it say that userscripts will no longer be supported (versus switching to manifest version 2)? – Brock Adams Nov 21 '12 at 12:08
  • @BrockAdams When a User script is converted to a Chrome extension, it's using manifest version 1. So, until the Chromium developers decide to use manifest v2, user scripts will fail to work at the end of next year. – Rob W Nov 21 '12 at 12:10
  • Why wouldn't the developers just change the code to convert using version 2? More importantly, has Google stated that userscripts will no longer be supported, because I can't find (so far) that they did.? – Brock Adams Nov 21 '12 at 12:15
  • @BrockAdams I haven't read any official source, but *eight* months have passed since manifest v2 was introduced; they've had plenty of time to update existing code. Until they do, the statement about native user script support is true (supported by the fact that user scripts still use manifest v1). – Rob W Nov 21 '12 at 12:29
  • Okay, it's good to know it's not official. As popular as userscripts are, I can't see Google dropping support for them so easily. Of course, they could just say, "Suck it up and install Tampermonkey." – Brock Adams Nov 21 '12 at 12:31
  • @BrockAdams Well, installing user script is a real pain in Chrome (compared to native extensions). Unless Chrome is running with the `--easy-off-store-extension-install` flag, users need to perform too many steps before they're able to use a userscript (Rightclick, Save, Open file browser, Open `chrome://extensions/`, Drag file to it, accept) vs Click & Accept (native extension in the Chrome Web Store). – Rob W Nov 21 '12 at 12:34
  • I am wondering if there is performance/memory advantage of using one over the other? I'm not sure but I think a TM user script will require less memory since there is no new extension and thus process created? But how much will be it? –  May 29 '16 at 09:36
  • @VarunAgw A user script doesn't require a new extension process. In Chrome, content script extensions require less resources than Tampermonkey because Tampermonkey has more overhead. – Rob W May 29 '16 at 09:39
  • @RobW No! I mean if I have TM already installed with several scripts already running. And thanks that was quick :) –  May 29 '16 at 09:43
  • @VarunAgw I don't know what you mean by "No", but my comment also applies if you already have Tampermonkey installed. Separate content script extensions have less overhead than a Tampermonkey user script. Tampermonkey makes it easier to install and use user scripts, but if you want to minimize resource usage then you'd better create an extension and use content scripts. – Rob W May 29 '16 at 09:48
  • @RobW Oh! I misunderstood `Tampermonkey has more overhead` as you were referring to TM extension. Got it anyway :) –  May 29 '16 at 10:20
3

The case for writing (and sharing!) user scripts: as long as you just use normal browser DOM APIs, your script can with minimal effort be picked up and made to run (often just installing it without edits) by anyone using another user script capable browser.

The case against them, when you use Google Chrome, is that as of Chrome 21, installing them got ridiculously messy: you need to right click the installation link, save it to disk, open the extensions page, and drag the saved script in there (yes), or install TamperMonkey from the Chrome Web Store (it is free, and does for Chrome essentially what Greasemonkey does for Firefox: gives user script installation and maintenance a bearable user interface).

While you only need to do the things you list, you may not need any more privileged API access than what user scripting gives you natively: try executing the XMLHttpRequest code you would be writing for the script in the devtools console when on another site and you'll see if it's enough for your needs or not. Many web APIs these days allow CORS support, which might save your use case from needing to resort to the proprietary browser-native extension APIs.

ecmanaut
  • 5,030
  • 2
  • 44
  • 66
-3

User scripts (ie. Greasemonkey scripts) installed directly with Chrome are eventually automatically converted to extensions with content scripts, so they're essentially the same. User scripts are quick and easier to write, and a good user script can be supported in other browsers. However, there are several limitations for user scripts in Chrome. They cannot directly read or write variables from webpages (only standard DOM is shared). They cannot make cross-origin XHRs. On the contrary, extensions are much more powerful but harder to develop.

It seems that you'll have to write a Chrome extensions as you need to 'make API calls to an external website'.

方 觉
  • 4,042
  • 1
  • 24
  • 28
  • 3
    No, Chrome userscripts **can** do "cross-origin XHRs" and they can, easily enough, read or write variables, using injected code. And they can also make API calls to an external websites. – Brock Adams Nov 21 '12 at 07:10