简体   繁体   English

Firefox扩展:将javascript添加到网页

[英]Firefox extension: Add javascript to webpage

I'm working on a FireFox extension that listens to onStateChange . 我正在研究侦听onStateChange的FireFox扩展。 When the current document has been loaded it should insert a script to the page and it should be able to call the script on a button event. 加载当前文档后,应在页面上插入脚本,并且应该能够在按钮事件中调用脚本。

Now I am able to add a button to all webpages by using: 现在,我可以使用以下命令向所有网页添加按钮:

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));

All functions return success, but no script is added to the page. 所有函数均返回成功,但没有脚本添加到页面中。 No alert is displayed, and if i insert a function and call it from the button.onclick then the FireFox log displayes that the function is not available. 没有警报显示,并且如果我插入一个函数并从button.onclick调用它,则FireFox日志显示该函数不可用。

If I use the exact same procedure from a javascript inside the html page, then it works find and the alert pops up. 如果我使用html页面中的javascript完全相同的过程,那么它可以找到并弹出警报。

Do I need to do anything to enable the script from my extension or why is the script not available from the button or anywhere else? 我需要做些什么来启用扩展程序中的脚本,或者为什么按钮或其他任何地方都无法使用该脚本?

I hate to say it after you created a bunch of code, but check out Greasemonkey: https://addons.mozilla.org/en-US/firefox/addon/748 在创建了一堆代码之后,我不想这么说,但是请查看Greasemonkey: https : //addons.mozilla.org/en-US/firefox/addon/748

It'll probably handle a lot of your work for you. 它可能会为您处理很多工作。

Yes, sounds like you're tryin to re-invent the wheel. 是的,听起来您正在尝试重新发明轮子。 Use Greasemonkey as Oren suggested. 按照Oren的建议使用Greasemonkey。

Here is a Greasemonkey script that I use to load external JS framework (Prototype and Scriptaculous in this case) load any number of external files (js and css) into a page. 这是一个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.

相关问题 在Firefox扩展上使用Javascript动态获取网页的URL - Get URL of a webpage dynamically with Javascript on a Firefox extension 如何从Firefox扩展名执行网页名称空间(范围)中的javascript代码? - How to execute javascript code in the namespace (scope) of the webpage, from a firefox extension? 检查浏览器是否正在加载网页的Javascript函数(Firefox扩展) - Javascript function that check if the browser is loading a webpage (Firefox Extension) 为什么FireFox插件中的JavaScript比FireFox加载的网页中的JavaScript慢? - Why is JavaScript slower in a FireFox add-on than in a webpage loaded in FireFox? 使用没有 Greasemonkey 的 Javascript 扩展使用 Firefox 扩展动态更改网页内容 - Change content of a webpage dynamically with a Firefox extension using Javascript without Greasemonkey 将功能添加到firefox扩展Javascript文件 - Add function to firefox extension Javascript file 为什么我的Firefox扩展程序不使用从Firefox 4的网页中提取的jQuery 1.4.2对象添加事件处理程序 - Why does my Firefox extension not add event handlers using jQuery 1.4.2 object pulled from webpage in Firefox 4 向Firefox扩展添加功能 - Add functionality to firefox extension 如何使用Javascript检查是否已安装Firefox 3插件/扩展程序 - How to check with Javascript if a Firefox 3 Add-on / Extension is installed javascript打开firefox扩展名 - javascript to open a firefox extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM