如何使用Firefox扩展插入JavaScript功能到所有网页 [英] How to inject a JavaScript function to all web page using Firefox extension

查看:123
本文介绍了如何使用Firefox扩展插入JavaScript功能到所有网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Firefox插件。我想要做的是注入一个自定义的JavaScript函数。
$ b $即

$ $ $ $ $ $ $ c $>




$ b $所以所有的页面都可以调用 foo ,而不是先定义它。



我从其他答案看,如: http://groups.google.com/group/greasemonkey-users/browse_thread/thread/3d82a2e7322c3fce



但是它需要修改网页。如果我想将 foo 功能注入Google.com,该怎么办?是否有可能这样做?

我可以用一个userscript来做,但是我想在可能的情况下使用扩展的方法。
div class =h2_lin>解决方案

如果在页面上使用javascript函数做一个简单的href。

像bookmarklets一样工作。
$ b $以下是一个示例代码:

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ var $ new $ $ $ $'使用document.createElement( '脚本');

// Math.random()部分用于避免缓存
newScript.src = scriptUrl +'?dummy ='+ Math.random();

//将新脚本附加到dom
document.body.appendChild(newScript);

//执行你的新函数
window.foo();
$ b $('[在线脚本的网址]')

<
要使用它,把你的脚本的网址。

它必须只有一行代码,url格式化,但为了代码的可读性,我已经将其格式化了。


我从来没有开发Firefox扩展, JavaScript注入,这就是我将如何滚动。


希望它有帮助。


I am developing a Firefox addon. What I want to do is to inject a custom JavaScript function.

i.e.

function foo() {..}

So all the pages can call the foo without define it first.

I have look from other answer such as: http://groups.google.com/group/greasemonkey-users/browse_thread/thread/3d82a2e7322c3fce

But it requires modification on the web page. What if perhaps I want to inject the function foo into Google.com? Is it possible to do so?

I can do it with a userscript, but I want to use the extension approach if possible.

解决方案

What if you make a simple href with javascript function on the page.
Like bookmarklets work.

Here is a sample code :

function(scriptUrl) {

    var newScript = document.createElement('script');

    // the Math.random() part is for avoiding the cache
    newScript.src = scriptUrl + '?dummy=' + Math.random();

    // append the new script to the dom
    document.body.appendChild(newScript);

    // execute your newly available function
    window.foo();

}('[url of your online script]')


To use it, put your script's url.
It must be only one line of code, url formated, but for code readability I've formated it.

I've never developed a Firefox extension, but for javascript injection that's how I would roll.

Hope it helped.

这篇关于如何使用Firefox扩展插入JavaScript功能到所有网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆