简体   繁体   English

如何从 Firefox 扩展执行页面定义的 JavaScript 函数?

[英]How do I execute a page-defined JavaScript function from a Firefox extension?

I'm creating a Firefox extension for demo purposes.我正在为演示目的创建一个 Firefox 扩展。 I to call a specific JavaScript function in the document from the extension.我从扩展中调用文档中的特定 JavaScript 函数。 I wrote this in my HTML document (not inside extension, but a page that is loaded by Firefox):我在我的 HTML 文档中写了这个(不是在扩展里面,而是一个由 Firefox 加载的页面):

document.funcToBeCalled = function() {
   // function body
};

Then, the extension will run this on some event:然后,扩展将在某些事件上运行它:

var document = Application.activeWindow.activeTab.document;
document.funcToBeCalled();

However it raises an error saying that funcToBeCalled is not defined.但是它会引发一个错误,指出funcToBeCalled未定义。

Note: I could get an element on the document by calling document.getElementById(id);注意:我可以通过调用document.getElementById(id);来获取文档上的一个元素document.getElementById(id);

It is for security reasons that you have limited access to the content page from extension.出于安全原因,您从扩展程序访问内容页面的权限有限。 See XPCNativeWrapper and Safely accessing content DOM from chrome ,请参阅XPCNativeWrapperSafely accessing content DOM from chrome

If you control the page, the best way to do this is set up an event listener in the page and dispatch an event from your extension (addEventListener in the page, dispatchEvent in the extension).如果您控制页面,最好的方法是在页面中设置一个事件侦听器并从您的扩展程序中调度事件(页面中的 addEventListener,扩展中的 dispatchEvent)。

Otherwise, see http://groups.google.com/group/mozilla.dev.extensions/msg/bdf1de5fb305d365否则,请参阅http://groups.google.com/group/mozilla.dev.extensions/msg/bdf1de5fb305d365

document.wrappedJSObject.funcToBeCalled();

This is not secure and allows a malicious page to elevate its permissions to those of your extension... But, it does do what you asked.并不安全,并且允许恶意页面将其权限提升到您的扩展程序的权限......但是,它确实按照您的要求执行。 Read up on the early greasemonkey vulnerabilities for why this is a bad idea.阅读早期的greasemonkey 漏洞,了解为什么这是一个坏主意。

I have a very simpler way to do it.我有一个非常简单的方法来做到这一点。 Suppose you have to call xyz() function which is written on page.假设您必须调用写在页面上的 xyz() 函数。 and you have to call it from your pluggin.你必须从你的插件中调用它。

create a button ("make it invisible. so it wont disturb your page").创建一个按钮(“让它不可见。这样它就不会打扰你的页面”)。 on onclick of that button call this xyz() function. onclick 该按钮调用此 xyz() 函数。

<input type="button" id="testbutton" onclick="xyz()" />

now in pluggin you have a document object for the page.现在在插件中你有一个页面的文档对象。 suppose its mainDoc假设它的 mainDoc

where you want to call xyz(), just execute this line在你想调用 xyz() 的地方,只需执行这一行

mainDoc.getElementById('testbutton').click();

it will call the xyz() function.它将调用 xyz() 函数。

good luck :)祝你好运 :)

You can do it, but you need to have control over the page and be able to raise the privilege level for the script.您可以这样做,但您需要控制页面并能够提高脚本的权限级别。 Mozilla Documentation gives an example - search for "Privilege" on the page. Mozilla 文档给出了一个例子——在页面上搜索“权限”。

var pattern = "the url you want to block";

function onExecuted(result) {
console.log(`We made it`);
}

function onError(error) {
console.log(`Error: ${error}`);
}

function redirect(requestDetails) {
var callbackName = 'callbackFunction'; //a function in content js
var data = getDictForkey('a url');
var funcStr = callbackName + '(' + data + ')';
const scriptStr = 'var header = document.createElement(\'button\');\n' +
    ' header.setAttribute(\'onclick\',\'' + funcStr + '\');' +
    ' var t=document.createTextNode(\'\');\n' +
    ' header.appendChild(t);\n' +
    ' document.body.appendChild(header);' +
    ' header.style.visibility="hidden";' +
    ' header.click();';
const executing = browser.tabs.executeScript({
    code: scriptStr
});
executing.then(onExecuted, onError);
return {
    cancel: true
}
}

chrome.webRequest.onBeforeRequest.addListener(
redirect,
{urls: [pattern]},
["blocking"]
);

function getDictForkey(url) {
xxxx
return xxxx;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM