[英]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.