Reputation: 1364
This sounds like a very simple question, but I've been going through the MDN documentation of "Firefox for Android" and I can't seem to figure it out.
I'm trying to build a simple "Firefox for Android" add-on, which simply inserts a script tag into visited pages. I've tried the following code (leaving out the template boilerplate code):
function loadIntoWindow(window) {
if (!window)
return;
menuId = window.NativeWindow.menu.add("Insert", null, function() {
insertScript(window);
});
}
function insertScript(window) {
// none of these properties exist: window.document.body.innerHTML, window.body.innerHTML
// window.document.body.innerHTML = "<h1>this page has been eaten</h1>";
// var contentScriptString = 'document.body.innerHTML = "<h1>this page has been eaten</h1>";'
// throws error "ReferenceError: require is not defined"
// var tabs = require("sdk/tabs");
// tabs.activeTab.attach({
// contentScript: contentScriptString
// });
// throws error "TypeError: attach is not a function"
// window.BrowserApp.selectedTab.attach({
// contentScript: contentScriptString
// });
}
function unloadFromWindow(window) {
if (!window)
return;
window.NativeWindow.menu.remove(menuId);
}
The loadIntoWindow function is being correctly called, since the "Insert" option is added as a menu option, and this option in turn correctly calls the insertScript option when tapped.
However, I'm at a loss on how to access the window's DOM in order to insert the script. I've tried options mentioned on the page about content scripts and the BrowserApp property..
Thanks,
William
Upvotes: 2
Views: 105
Reputation: 1364
Looks like I can answer my own question. After looking through browser.js, I found that the following code works to access the current page's DOM:
var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
var document = contentWindow.document;
document.body.innerHTML = "<h1>this page has been eaten</h1>";
Upvotes: 1