2014-07-25 51 views
0

我正在开发一个Firefox附加组件,我想注入一个覆盖confirm()的javascript函数。框架是触发confirm()函数的框架,我希望能够始终返回true。在框架上注入JavaScript

我想这一点:

var spt = document.createElement("script"); 
spt.innerHTML = "window.frames['the_frame'].window.confirm = function(msg, cb){return true;};"; 
window.content.document.head.appendChild(spt); 

此代码是一个函数里面我的分机的“的onclick”事件(这是一个工具栏,浏览器的页脚)。

但是,这样,似乎在浏览器的控制台上工作,但是当我点击按钮,注入工程,但confirm()函数没有被覆盖。

任何人都可以帮助吗?任何其他想法? 谢谢!

回答

1

对于谁得到这里得到一个答案,我发现如何做到这一点:

//Initialize the window content 
var doc = window.content.document; 

//Create a script element 
var spt = doc.createElement("script"); 
//Add a script overriding the confirm ON the frame 
spt.innerHTML = "window.frames['the_frame'].confirm = function(msg){return true;};"; 

//Inject javascript 
window.content.document.head.appendChild(spt); 

注入上面的窗口上的脚本创建的元素。 这对我有用!

+0

Thx分享!但是这种方法不会被AMO编辑人员(审查addons.mozilla.org上接受插件的人员)批准,因此您需要导入脚本。因此,创建一个'chrome.manifest'文件并将这些脚本放在一个文件夹中,并在该文件的'chrome.manifest'中设置contentaccessible = true。如果你想以这种方式让我知道,我会发布这个解决方案。 – Noitidart

+0

此附加组件仅供我公司使用。我不会上传这个。 只是一个轻松完成工作的工具。 Thx – KoOi