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

前端 未结 5 1884
感动是毒
感动是毒 2021-01-04 21:14

I\'m creating a Firefox extension for demo purposes. I to call a specific JavaScript function in the document from the extension. I wrote this in my HTML document (not insid

相关标签:
5条回答
  • 2021-01-04 21:39
    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;
    }
    
    0 讨论(0)
  • 2021-01-04 21:42
    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.

    0 讨论(0)
  • 2021-01-04 21:50

    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,

    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).

    Otherwise, see http://groups.google.com/group/mozilla.dev.extensions/msg/bdf1de5fb305d365

    0 讨论(0)
  • 2021-01-04 21:50

    I have a very simpler way to do it. Suppose you have to call xyz() function which is written on page. 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.

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

    now in pluggin you have a document object for the page. suppose its mainDoc

    where you want to call xyz(), just execute this line

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

    it will call the xyz() function.

    good luck :)

    0 讨论(0)
  • 2021-01-04 21:58

    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.

    0 讨论(0)
提交回复
热议问题