Nw.js: webview.addContentScripts seems running in a different context

Created on 29 Nov 2015  路  4Comments  路  Source: nwjs/nw.js

addContentScripts([
{
name: 'myRule',
matches: ['< all_urls >'],
js: {files: ['test.js']},
run_at: 'document_end'
}]);
Simple test test.js:
window.abc="nwjs";
console.log(abc)
//output nwjs

type 'abc'/'window.abc' in the webview debug console, output undefined.

Cannot override/share variables with page context.

Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

question stale

Most helpful comment

Hi,

I am sending messages back and forth:

In the loader of the WebView:

    webview.addEventListener('contentload', function() {
        webview.executeScript({code: setGuestInfoJS()});

        window.setTimeout(function() {
            webview.contentWindow.postMessage('handshake', '*');
        },200);
        window.addEventListener("message", function(event) {
            statusBarLink(event.data)
        });
    });

and the setGuestInfoJs() returns the whole script to get a message, if the mouse is over a link... this is very "rude", because we fire events on every mouse move and not best practice, but gets the job done:

function setGuestInfoJS() {
    var guestScript = "\
    function _ptReceiveMessage(event) {\
        appWindow = event.source;\
        appOrigin = event.origin;\
        window.addEventListener('mousemove', function(e) {\
            var ptX = e.clientX, ptY = e.clientY, elementMouseIsOver = document.elementFromPoint(ptX, ptY);\
            var elementName=elementMouseIsOver.nodeName;\
            var elementParent=elementMouseIsOver.parentNode;\
            var elementParentName=elementParent.nodeName;\
            if(elementName=='A' || elementParentName=='A') {\
                if(elementName=='A') {\
                    var ptLinkTarget = elementMouseIsOver.getAttribute('href');\
                }\
                if(elementParentName=='A') {\
                    var ptLinkTarget = elementParent.getAttribute('href');\
                }\
                _ptSendMessage(ptLinkTarget);\
            }\
        });\
    }\
    function _ptSendMessage(data) {\
        if (!appWindow || !appOrigin) {\
            console.log('Cannot send message to Chrome wrapper app - communication channel has not yet been opened');\
        }\
        appWindow.postMessage(data, appOrigin);\
    }\
    window.addEventListener('message', _ptReceiveMessage);\
    ";
    return guestScript;
}

All 4 comments

After some initial testing, I think I found out that:

webview.contentWindow is not the same window object that the js can get inside the page of the webview, although they share the same dom.

All executed/injected code will be in contentWindow which does not effect the real js running in that page at all.

Is this the design of Chrome App? But how can we communicate with the webview?

+1

Hi,

I am sending messages back and forth:

In the loader of the WebView:

    webview.addEventListener('contentload', function() {
        webview.executeScript({code: setGuestInfoJS()});

        window.setTimeout(function() {
            webview.contentWindow.postMessage('handshake', '*');
        },200);
        window.addEventListener("message", function(event) {
            statusBarLink(event.data)
        });
    });

and the setGuestInfoJs() returns the whole script to get a message, if the mouse is over a link... this is very "rude", because we fire events on every mouse move and not best practice, but gets the job done:

function setGuestInfoJS() {
    var guestScript = "\
    function _ptReceiveMessage(event) {\
        appWindow = event.source;\
        appOrigin = event.origin;\
        window.addEventListener('mousemove', function(e) {\
            var ptX = e.clientX, ptY = e.clientY, elementMouseIsOver = document.elementFromPoint(ptX, ptY);\
            var elementName=elementMouseIsOver.nodeName;\
            var elementParent=elementMouseIsOver.parentNode;\
            var elementParentName=elementParent.nodeName;\
            if(elementName=='A' || elementParentName=='A') {\
                if(elementName=='A') {\
                    var ptLinkTarget = elementMouseIsOver.getAttribute('href');\
                }\
                if(elementParentName=='A') {\
                    var ptLinkTarget = elementParent.getAttribute('href');\
                }\
                _ptSendMessage(ptLinkTarget);\
            }\
        });\
    }\
    function _ptSendMessage(data) {\
        if (!appWindow || !appOrigin) {\
            console.log('Cannot send message to Chrome wrapper app - communication channel has not yet been opened');\
        }\
        appWindow.postMessage(data, appOrigin);\
    }\
    window.addEventListener('message', _ptReceiveMessage);\
    ";
    return guestScript;
}

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chino23 picture chino23  路  3Comments

daverave1212 picture daverave1212  路  3Comments

imyzf picture imyzf  路  4Comments

xzh-loop picture xzh-loop  路  3Comments

niutech picture niutech  路  4Comments