Firefox扩展:将javascript添加到网页

时间:2009-10-30 14:54:14

标签: javascript firefox-addon

我正在处理一个侦听onStateChange的FireFox扩展程序。当加载当前文档时,它应该向页面插入一个脚本,它应该能够在按钮事件上调用脚本。

现在,我可以使用以下方法为所有网页添加按钮:

nsCOMPtr<nsIDOMElement> NewInputElementTest;
rv = htmlDoc->CreateElement(NS_LITERAL_STRING("input"),getter_AddRefs(NewInputElementTest));

rv = NewInputElementTest->SetAttribute(NS_LITERAL_STRING("type"),NS_LITERAL_STRING("button"));

rv = NewInputElementTest->SetAttribute(NS_LITERAL_STRING("value"),NS_LITERAL_STRING("hummer"));

rv = body->AppendChild(NewInputElementTest,getter_AddRefs(AddedNewInputElement2));

The button is displayed correctly.


I wish to use the same procedure to add a SCRIPT to the page, like so:

rv = htmlDoc->CreateElement(NS_LITERAL_STRING("script"),getter_AddRefs(NewInputElement));
rv = NewInputElement->SetAttribute(NS_LITERAL_STRING("type"),NS_LITERAL_STRING("text/javascript"));
rv = NewInputElement->SetAttribute(NS_LITERAL_STRING("text"),NS_LITERAL_STRING("alert('hello world!')"));
rv = body->AppendChild(NewInputElement,getter_AddRefs(AddedNewInputElement));

所有函数都返回成功,但没有脚本添加到页面中。没有显示警报,如果我插入一个函数并从button.onclick中调用它,则FireFox日志显示该函数不可用。

如果我在html页面内的javascript中使用完全相同的程序,那么它就可以找到并弹出警告。

我是否需要执行任何操作才能从我的扩展程序中启用脚本,或者为什么脚本无法在其他任何位置启用脚本?

2 个答案:

答案 0 :(得分:2)

我不想在你创建一堆代码后说出来,但请查看Greasemonkey:https://addons.mozilla.org/en-US/firefox/addon/748

它可能会为你处理很多工作。

答案 1 :(得分:1)

是的,听起来你正试图重新发明轮子。按照Oren的建议使用Greasemonkey。

这是一个Greasemonkey脚本,用于加载外部JS框架(在本例中为Prototype和Scriptaculous),将任意数量的外部文件(js和css)加载到页面中。

// ==UserScript==
// @name           External Loader
// @namespace      http://ifelse.org
// @description    Loads external JS and CSS
// @include        http://*.yoursitedomainetc.com/*
// ==/UserScript==

var hasPrototype  = ('Prototype' in unsafeWindow);
var hasEffects    = ('Effect'    in unsafeWindow);

function _require(url, isCSS) {
    if (isCSS) {
        var script = document.createElement('link');
        script.setAttribute('type', 'text/css');
        script.setAttribute('rel',  'stylesheet');
        script.setAttribute('href', url);
    } else {
        var script = document.createElement('script');
        script.setAttribute('type',    'text/javascript');
        script.setAttribute('charset', 'UTF-8');
        script.src = url;
    }
    document.getElementsByTagName('head')[0].appendChild(script);
}

//  Load prototype; shouldn't get here because it is already on the page
if ( !hasPrototype ) {
    _require('http://path.com/to/prototype/1.6.0.2/prototype.js');
}

//  Load scriptaculous effects if it's not already loaded
if ( !hasEffects ) {
    _require('http://path.com/to/scriptaculous/1.8.1/effects.js');
}

//  Add greasemonkey ajax object
//  Copies format of Prototype Ajax.Request to
//  Allow to easily swap out at a later point (i.e. no longer FF plugin)
unsafeWindow.Remote = new Object;
unsafeWindow.Remote.Ajax = function(url, options) {
    if (options.onCreate) {
        options["onCreate"]();
    }

    var request = {
        method: options.method || 'get',
        url: url + ('?' + unsafeWindow.Object.toQueryString(options.parameters) || ''),
        onload: function(response) {
            if (response.status == 200)
            options["onComplete"](response);
            options["onSuccess"]();
        },
        onerror: options.onFailure || null
    };
    window.setTimeout(GM_xmlhttpRequest, 0, request);
};

//  Load these External files
_require('http://path/to/anything/and/dont/cache/it.js' + '?cache=' + (new Date()).getTime());
_require('http://paht/to/something/else.css', true);
}