javascript - Polyfill window.showModalDialog in webview -


so creating chrome app embedded <webview> element. embedded webapp contains lot of legacy code such window.showmodaldialog calls (which chrome no longer supports).

the thing i'm trying polyfill calls. i've created simple test example:

<webview src="http://legacycodeisawesome.com/" style="width:100%; height:100%"></webview> 

code operating on webview element:

webview.addeventlistener('contentload', function() {     webview.executescript(       {         code: 'window.showmodaldialog = function() { console.log ("hello, world");}',         runat: 'document_start'       }     );   }); 

the above code runs (adding debug console.log works), doesn't it's supposed do, overwrite showmodaldialog function.

is there sort of restriction on overwriting functions on window object in webviews? there way around it?

when call webview.executescript, create content script.

one of core principles of content scripts isolated world: content script sees separate copy of window object.

content scripts execute in special environment called isolated world. have access dom of page injected into, not javascript variables or functions created page. looks each content script if there no other javascript executing on page running on. same true in reverse: javascript running on page cannot call functions or access variables defined content scripts.

so, override works, in content script context.

to perform override in page context itself, we need go deeper. we need go deeper

a content script can create <script> element on page; code in said element execute in page's context. can read technique, called page-level scripting or injected scripts, in canonical question.

// content script var script = document.createelement('script'); script.textcontent = `   window.showmodaldialog = function() {     console.log("hello, world");   } `; (document.head||document.documentelement).appendchild(script); script.remove(); 

that said, you're injecting code far late. on contentload offending js executed already, , adding "document_start" isn't going rewind time.

fortunately, can declare in advance content scripts want have:

webview.addcontentscripts([   {     name: "showmodaldialogpolyfill",     matches: ["https://webapp.example.com/*"],     js: { files: ['content.js'] },     run_at: 'document_start'   } ]); webview.src = "https://webapp.example.com/"; 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -