简体   繁体   English

“ Chrome应用” webview.executeScript访问来宾全局变量

[英]“Chrome Apps” webview.executeScript access guest global varibles

I can access a Chrome App Webview HTML with: 我可以通过以下方式访问Chrome App Webview HTML:

webview.executeScript(
    {code: 'document.documentElement.innerHTML'},
    function(results) {
      // results[0] would have the webview's innerHTML.
    });

But I would like to get the value of global variables in the Guest like so: 但是我想像这样在Guest中获取全局变量的值:

webview.executeScript(
   {code: 'window.globalVar'},
   function(results) {
   // results[0] should have the webview's value of "globalVar".
   });

How can I do this? 我怎样才能做到这一点?

An answer to summarize the steps required. 总结所需步骤的答案。

1) You inject a content script with webview.executeScript() into the embedded page. 1)您将带有webview.executeScript()的内容脚本注入到嵌入式页面中。

2) Since the page's real window is isolated, you need a page-level script to access it. 2)由于页面的实际window是隔离的,因此需要页面级脚本才能访问它。 You inject it with a <script> tag as discussed here . 你用它注入<script>标记作为讨论在这里

3) The page-level script can access the window object, but cannot talk to the app script. 3)页面级脚本可以访问window对象,但不能与应用程序脚本对话。 However, it can fire a custom DOM event, that the content script can catch. 但是,它可以触发内容脚本可以捕获的自定义DOM事件。 Discussed here . 在这里讨论。

4) Finally, from the content script you need to send a message to your app script. 4)最后,您需要从内容脚本向应用程序脚本发送消息。 The content script calls chrome.runtime.sendMessage , while the app script listens with chrome.runtime.onMessage . 内容脚本调用chrome.runtime.sendMessage ,而应用程序脚本使用chrome.runtime.onMessage监听。 chrome.runtime.sendMessage does not seem to be available to webview content scripts injected with webview.executeScript() . chrome.runtime.sendMessage似乎不适用于通过webview.executeScript()注入的webview.executeScript()内容脚本。 A workaround is to use postMessage as described here . 解决方法是按此处所述使用postMessage

It's a bit of an onion structure, that's why you need 2 steps "in" and 2 steps "out". 这有点像洋葱结构,这就是为什么您需要2步“入”和2步“出”的原因。 You can't really do it in the return value that's passed to the executeScript callback, since at least one of the "out" steps will be asynchronous. 您实际上无法在传递给executeScript回调的返回值中执行此操作,因为至少“输出”步骤之一将是异步的。

You can inject a script that inserts a DOM node with the global variable's value. 您可以插入一个脚本,该脚本插入具有全局变量值的DOM节点。 Then you return that node's innerHTML and you have your value right away without using a callback: 然后,您返回该节点的innerHTML,并且无需使用回调即可立即获得值:

var code = "script = document.createElement('script'); script.text=\"var n=document.createElement('span');n.style.display='none';n.id='my-id';n.innerHTML=window.globalVar;document.body.appendChild(n)\"; document.head.appendChild(script);document.getElementById('my-id').innerHTML"

webview.executeScript(
    {code: code},
    function(results) {
      console.log(results[0]);
    });

Just use an ID for the DOM node that is not used and you should be fine. 只需为未使用的DOM节点使用ID,就可以了。 It works for me. 这个对我有用。

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

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