在 Firefox 中访问 DOM for Android 附加组件

Accessing the DOM in a Firefox for Android add-on

这听起来像是一个非常简单的问题,但我一直在浏览 "Firefox for Android" 的 MDN 文档,但我似乎无法弄明白。

我正在尝试构建一个简单的 "Firefox for Android" 附加组件,它只是将脚本标记插入到访问过的页面中。我尝试了以下代码(省略了 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);
}

正确调用了 loadIntoWindow 函数,因为 "Insert" 选项被添加为菜单选项,并且该选项在点击时又正确调用了 insertScript 选项。

但是,我不知道如何访问 window 的 DOM 以插入脚本。我尝试了关于 content scripts and the BrowserApp property..

页面上提到的选项

谢谢,

威廉

看来我可以回答我自己的问题了。翻了翻browser.js,发现下面的代码可以访问当前页面的DOM:

var contentWindow = window.BrowserApp.selectedBrowser.contentWindow;
var document = contentWindow.document;

document.body.innerHTML = "<h1>this page has been eaten</h1>";