简体   繁体   中英

Reverse engineering a Firefox extension

I'm a young developer and am just trying my hand at JavaScript for the first time this week after coming from a background in Java. I found a nice add-on for Firefox that allows users to copy links from selected text. Its relatively small but I'd like to try configure it so it runs from a button rather than a (rightclick/select event)

DISCLAIMER: The extension in question is open source and licensed under GNU General Public License, version 2.0

I'm not reverse engineering anyone's hard work, I'm just trying to figure out what makes add-ons tick and how to apply its workings to something that's not an add-on.

The source is available here: https://addons.mozilla.org/en-US/firefox/files/browse/77730/ (it's quite small, just one jar so don't be afraid to read)

Back to the point: it's a neat little add-on that allows users to select multiple links in a web page and copy/paste the links selected by right clicking the text and selecting copy selected links.

So the 2 parts of my question:

  1. How do I run this type of add-on from a button instead of a right click? (I'm assuming it has something to do with the EOL function)

  2. Is there anything I need to be aware of when converting add-ons other than cross browser compatibility? Should I be contacting the author for help or would that offend?

Any and all help appreciated!

* *

I'm pretty sure I'm passing 'true' to welcome for no reason. Also pretty sure turning this into an internal type script My small attempt at taking the source and making something out of it:

HTML:

<input type="button" id="grablinkstest" value="CopyLinks" onclick="welcome(true);"/>

JAVASCRIPT:

function welcome() {
    alert("welcome command works");
    var focusedWindow = document.commandDispatcher.focusedWindow;
    var focusedDoc = document.commandDispatcher.focusedWindow.document;
    var argc = gCopyLinks.GetLinks.arguments.length;
    var argv = gCopyLinks.GetLinks.arguments;
    var reMask;

    var selLinks = [];
    for( i = 0; i < focusedDoc.links.length; i++) {
        if((!bSelected || focusedWindow.getSelection().containsNode(focusedDoc.links[i], true)) && (argc <= 1 || focusedDoc.links[i].href.match(reMask))) {
            selLinks[j] = focusedDoc.links[i].href;
            j++;
        }
    }

}

How do I run this type of add-on from a button instead of a right click? (I'm assuming it has something to do with the EOL function)

No, it has nothing to do with the EOL function - this function merely determines the end-of-line character sequence for the operating system used. Extension's overlay calls gCopyLinks.OnCommand('all') if its menu item is clicked, I guess you want to add a button doing the same. See https://developer.mozilla.org/en/XUL_School/Adding_Toolbars_and_Toolbar_Buttons on adding toolbar buttons in extensions.

Is there anything I need to be aware of when converting add-ons other than cross browser compatibility?

Yes. Extensions have special privileges that web content doesn't have. In particular, there is currently no API allowing web applications to copy data to the clipboard. However, one is proposed and being worked on .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM