Reputation: 1663
I have developed a very powerful and complicated bookmarklet, but it's starting to bump into the edges of the bookmarklet format. I'm considering turning it into a browser extension. For Chrome, I found this tool that makes it easy to get started. I'll need to tweak the outputs a bit, but it's fairly simple.
I'm wondering if there's something similar for Firefox. I want an extension that adds a button to the browser, and when you click it, it runs a script on the page. Can anyone point me to a tutorial, or the related part of the documentation?
Upvotes: 1
Views: 520
Reputation: 37228
Bookmarklets are very simple. The WebExtension API is not good if you want to use the addon right away.
The SDK is guaranteed to work right now, and the level of features which you will use from the SDK for a bookmarklet will continue to work in the future.
It's quite easy. What you want to do is follow this guide: https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_Started_%28jpm%29
Do the prerequisites (install node [which installs npm]) should take just a few minutes.
Then when you get to the Implementing the add-on
section of that guide. Don't paste what they say, instead paste this:
var data = require("sdk/self").data;
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.mozilla.org",
contentScriptFile: [data.url("jquery.js"),
data.url("my-script.js")]
});
Modify the include
to match the website you want to target.
Then create a folder next to your index.js
file, call it data. Inside, create my-script.js
and paste your bookmarklet. If you need jquery then add that in there as well. If you don't, then remove jquery from the contentScriptFile
line in the snippet i pasted above.
Then type in npm jpm xpi
in command line (as thats how you use jpm).
To test - Make sure to go about:config and set xpinstall.signatures.required
to false, as signing is required for Firefox now. Then drag that xpi to your Firefox.
Then when ready, upload that XPI to addons.mozilla.org :)
Upvotes: 3
Reputation: 6206
Starting at version 42 (current stable version), Firefox includes support for WebExtensions, ie the same API as Chrome Extensions, so you can luckily use the same tool.
Take into account that it's work in progress and it's not 100% ported, so please read the docs about the state of it.
The current Addon SDK will be deprecated in the future in favor of WebExtensions, so I think you should give WebExtensions a try, even more when it allows you to only have to maintain one code base.
Upvotes: 1