user3652242
user3652242

Reputation:

Access to the elements of the page in the Page-Worker

I've created a page-worker in the extension

dup = pageWorker.Page({
    contentScript: "self.port.on('alert', function(message) {"+
        "console.log(message);"+
        "document.querySelector('.test-element').title = message;"+
    "});",
    contentScriptWhen: "ready",
    contentURL: "http://example.com/Licznik-beta/addon.html"
});

In "contentScript" I can relate to "document". But I can not relate to the window, or function, or variable. console.log(window) in contentScript return "TypeError: cyclic object value timers.js:43".

I do not understand how it works. Can someone explain to me?

How to change it?

EDIT

I've added a few lines to the test:

self.port.on('addon-licznik', function () {

    console.log(document);
    console.log(window); //  TypeError: cyclic object value   timers.js:43
    runFromAddon(); // ReferenceError: runFromAddon is not defined   timers.js:43

});

Function: runFromAddon(); Of course there is.

Second test:

function funSet (tresc) {
    var addonScript = document.querySelector(".addon-script");
    if ( addonScript != undefined ) {
        document.querySelector('head').removeChild( addonScript );
    }
    var script = document.createElement("script");
    script.className = "addon-script";
    script.textContent = tresc;
    document.querySelector('head').appendChild(script);
}

function marmo (message) {
    console.log(message);

    funSet("console.log(window); runFromAddon();");
}


self.port.on('addon-licznik', marmo);

It works well.

Window → http://example.com/Licznik-beta/addon.html
runFromAddon-Log

Upvotes: 0

Views: 271

Answers (1)

willlma
willlma

Reputation: 7543

If you're writing the HTML yourself, then use addon instead of self and attach the script to the page using <script></script> instead of contentScript(File). See Scripting trusted page content.

If you're not writing the HTML, then see Communicating with Page Scripts.

Upvotes: 1

Related Questions