Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Userscripts vs. Chrome Extension [closed]

Tags:

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?

like image 957
Avi Meir Avatar asked Nov 21 '12 06:11

Avi Meir


2 Answers

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

  • http://developer.chrome.com/extensions/content_scripts.html
  • http://developer.chrome.com/extensions/match_patterns.html
like image 174
Rob W Avatar answered Oct 17 '22 06:10

Rob W


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.

like image 22
ecmanaut Avatar answered Oct 17 '22 05:10

ecmanaut