繁体   English   中英

Firefox扩展:将javascript添加到网页

[英]Firefox extension: Add javascript to webpage

我正在研究侦听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完全相同的过程,那么它可以找到并弹出警报。

我需要做些什么来启用扩展程序中的脚本,或者为什么按钮或其他任何地方都无法使用该脚本?

在创建了一堆代码之后,我不想这么说,但是请查看Greasemonkey: https : //addons.mozilla.org/en-US/firefox/addon/748

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

是的,听起来您正在尝试重新发明轮子。 按照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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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