Firefox扩展:向网页添加javascript
我正在开发一个FireFox扩展,它监听Firefox扩展:向网页添加javascript,javascript,firefox-addon,Javascript,Firefox Addon,我正在开发一个FireFox扩展,它监听onStateChange。加载当前文档后,它应该向页面插入脚本,并且应该能够在按钮事件中调用脚本 现在,我可以通过以下方式向所有网页添加按钮: nsCOMPtr<nsIDOMElement> NewInputElementTest; rv = htmlDoc->CreateElement(NS_LITERAL_STRING("input"),getter_AddRefs(NewInputElementTest)); rv = NewI
onStateChange
。加载当前文档后,它应该向页面插入脚本,并且应该能够在按钮事件中调用脚本
现在,我可以通过以下方式向所有网页添加按钮:
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));
nsCOMPtr新输入元素测试;
rv=htmlDoc->CreateElement(NS_LITERAL_STRING(“输入”),getter_AddRefs(NewInputElementTest));
rv=NewInputElementTest->SetAttribute(NS_LITERAL_字符串(“类型”),NS_LITERAL_字符串(“按钮”);
rv=NewInputElementTest->SetAttribute(NS_LITERAL_STRING(“value”)、NS_LITERAL_STRING(“hummer”);
rv=body->AppendChild(NewInputElementTest,getter_AddRefs(AddedNewInputElement2));
按钮显示正确。
我希望使用相同的过程向页面添加脚本,如下所示:
rv=htmlDoc->CreateElement(NS_LITERAL_STRING(“脚本”),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));
所有函数都返回success,但没有向页面添加脚本。没有显示警报,如果我插入一个函数并从button.onclick调用它,那么FireFox日志会显示该函数不可用
如果我在html页面内的javascript中使用完全相同的过程,那么它可以工作并弹出警报
我需要做些什么才能从我的扩展中启用脚本,或者为什么该脚本不能从按钮或其他任何地方使用?我不想在您创建了一堆代码后再这么说,但请查看Greasemonkey:
它可能会帮你处理很多工作。是的,听起来你想重新发明轮子。按照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);
}
谢谢你的帮助。我是新手,也许你能进一步帮助我。我需要从另一个应用程序或光盘上的脚本加载脚本,而不是从URL加载。这是一个问题吗?我的应用程序/脚本的用户是否需要事先安装greasemonkey?附加的脚本,在加载任何页面时,我应该将其放置在何处以便加载?如果你知道要包含的特定脚本或代码,并且不需要远程加载,那么你可以将上面的脚本替换为它的内容并让它运行。用户通常需要安装greasemonkey和要运行的脚本,但您也可以将greasemonkey脚本编译到Firefox插件中。所以让我直说吧。我需要创建一个扩展名来加载文件ie.c:\myscript.js,并将该脚本插入FireFox访问的任何网页。通过创建GreaseMonkey脚本并将其编译成xpi文件,是否可以实现这一点?一旦我有了xpi文件,我该怎么处理它呢?谢谢。XPI文件是Firefox扩展,您和用户可以像添加任何其他扩展一样将其添加到Firefox。直接从Firefox扩展执行时,_require函数运行良好(对特定选项卡使用
contentDocument
,而不是文档
)。似乎使用greasemonkey需要已安装该插件。没有办法用我已经加载的插件添加我自己的脚本吗?@OrenMazor:我非常不喜欢greasmonkey的想法。只有当页面完成加载时,才会加载脚本。在许多网站上,页面加载永远不会结束,因此脚本永远不会启动(这是greasmonkey设计)。重要的是在过去(他们不再这样做)使用更细粒度的控件和Opera人员。如果用户需要等待长达50分钟,他们会非常生气。可能会重复