I want to write Firefox Addon, that when certain page is loaded, e.g : www.google.com, dynamically create a button in the DOM, that when I click on it, it takes me to another page, for example: www.stackoverflow.com.
Note: this button should appear only when google.com is loaded.
I searched to get some answers, so far nothing works, any help will be appreciated. Thanks.
Edit:
Thank you very much, that was very helpful. Can you please direct me how i'm doing the same thing in chrome extension? i'm tried with manifest.json and js file, but got no luck!
This is quite simple.
Assuming your 'page modification function' looks something like this:
function install_my_button(domWindow) {
// insert your button here (e.g: into domWindow.document.body)
var button = domWindow.document.createElement('BUTTON');
button.value = 'OTHER DOMAIN';
domWindow.document.appendChild(button);
button.addEventListener('click', function(evt) {
domWindow.location.href = 'http://stackoverflow.com';
}, false, true); // yes - 4 arguments!
// that will place the button at the bottom of the page
// you may wish to use some CSS to position it, or perhaps
// find a specific container element within the page in
// which to place it.
// prevent multiple handling
domWindow.__my_button_installed__ = 1;
}
Then define a 'load' event handler which looks something like this (take note that this is where we limit this code to working on the target domain (e.g: google.com):
const Cc = Components.classes;
const Ci = Components.interfaces;
let consoleService = Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService);
function LOG(msg) { consoleService.logStringMessage("EXTENSION: "+msg); }
let wm = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
var my_load_handler = function (evt) {
try {
var browserEnumerator = wm.getEnumerator("navigator:browser");
while (browserEnumerator.hasMoreElements()) {
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.gBrowser;
var numTabs = tabbrowser.browsers.length;
for (var index = 0; index < numTabs; index++) {
var currentBrowser = tabbrowser.getBrowserAtIndex(index);
var domWindow = currentBrowser.contentWindow.wrappedJSObject;
// identify your target page (google.com or www.google.com)
if (/\/\/(?:www\.)?google.com/.test(domWindow.location.href)) {
// install the privileged method (if it's not already there!)
if (!domWindow.hasOwnProperty('__my_button_installed__') {
install_my_button(domWindow);
}
}
}
}
} catch (e) {
LOG(e);
}
}
You'll need to run that for all (current & future) chromeWindow elements:
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
WindowListener.setupBrowserUI(domWindow);
}
wm.addListener(WindowListener);
where WindowListener
is defined as:
var WindowListener = {
setupBrowserUI: function(window, xulWindow, othWindow) {
window.gBrowser.addEventListener('load', my_load_handler, true);
},
tearDownBrowserUI: function(window) {
window.gBrowser.removeEventListener('load', my_load_handler, true);
},
onOpenWindow: function(xulWindow) {
let domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
domWindow.addEventListener("load", function listener() {
domWindow.removeEventListener("load", listener, false);
var domDocument = domWindow.document.documentElement;
var windowType = domDocument.getAttribute("windowtype");
if (windowType == "navigator:browser")
WindowListener.setupBrowserUI(domWindow);
}, false);
},
onCloseWindow: function(xulWindow) { },
onWindowTitleChange: function(xulWindow, newTitle) { }
};
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments