Pomax: Let's make a Firefox Extension, the painless way |
Ever had a thing you really wanted to customise about Firefox, but you couldn't because it wasn't in any regular menu, advanced menu, or about:config?
For instance, you want to be able to delete elements on a page for peace of mind from the context menu. How the heck do you do that? Well, with the publication of the new node-based jpm, the answer to that question is "pretty dang simply"...
Let's make our own Firefox extension with a "Delete element" option added to the context menu:
We're going to make that happen in five steps.
npm install -g jpm
(make sure you have node.js installed) and done (this is mostly prerequisite to developing an extension, so you only have to do this once, and then never again. For future extensions, you start at step 2!)jpm init
to set up the standard files necessary to build your extension. Good news: it's very few files!index.js
file that command generated, writing whatever code you need to do what you want to get done,.xpi
extension by running : jpm xpi
,.xpi
file with FirefoxOf course, step (3) is the part that requires some effort, but let's run through this together. We're going to pretty much copy/paste the code straight from the context menu API documentation:
// we need to make sure we have a hook into "things" we click on:
1: var self = require("sdk/self");
// and we'll be using the context menu, so let's make sure we can:
2: var contextMenu = require("sdk/context-menu");
// let's add a menu item!
3: var menuItem = contextMenu.Item({
// the label is pretty obvious...
4: label: "Delete Element",
// the context tells Firefox which things should have this in their context
// menu, as there are quite a few elements that get "their own" menu,
// like "the page" vs "an image" vs "a link". .. We pretty much want
// everything on a page, so we make that happen:
5: context: contextMenu.PredicateContext(function(data) { return true; }),
// and finally the script that runs when we select the option. Delete!
6: contentScript: 'self.on("click", function (node, data) { node.outerHTML = ""; });'
});
The only changes here are that we want "delete" for everything, so the context is simply "for anything that the context menu opens up on, consider that a valid context for our custom script" (which we do by using the widest context possible on line 5), and of course the script itself is different because we want to delete nodes (line 6).
The contentScript property is a string, so we're a little restricted in what we can do without all manner of fancy postMessages, but thankfully we don't need it: the addon mechanism will always call the contentScript function with two arguments, "node" and "data, and the "node" argument is simply the HTML element you clicked on, which is what we want to delete. So we do! We don't even try to be clever here, we simply set the element's .outerHTML property to an empty string, and that makes it vanish from the page.
If you expected more work, then good news: there isn't any, we're already done! Seriously: run jpm run
yourself to test your extension, and after verifying that it indeed gives you the new "Delete element" option in the context menu and deletes nodes when used, move on to steps (4) and (5) for the ultimate control of your browser.
Because here's the most important part: the freedom to control your online experience, and Firefox, go hand in hand.
Комментировать | « Пред. запись — К дневнику — След. запись » | Страницы: [1] [Новые] |