1. <tfoot id='04VDa'></tfoot>

      1. <i id='04VDa'><tr id='04VDa'><dt id='04VDa'><q id='04VDa'><span id='04VDa'><b id='04VDa'><form id='04VDa'><ins id='04VDa'></ins><ul id='04VDa'></ul><sub id='04VDa'></sub></form><legend id='04VDa'></legend><bdo id='04VDa'><pre id='04VDa'><center id='04VDa'></center></pre></bdo></b><th id='04VDa'></th></span></q></dt></tr></i><div id='04VDa'><tfoot id='04VDa'></tfoot><dl id='04VDa'><fieldset id='04VDa'></fieldset></dl></div>
      2. <legend id='04VDa'><style id='04VDa'><dir id='04VDa'><q id='04VDa'></q></dir></style></legend>

          <bdo id='04VDa'></bdo><ul id='04VDa'></ul>

        <small id='04VDa'></small><noframes id='04VDa'>

      3. window.open 在 chrome 扩展中返回 undefined

        window.open returns undefined in chrome extension(window.open 在 chrome 扩展中返回 undefined)

          <tfoot id='1Yu0s'></tfoot>
              <tbody id='1Yu0s'></tbody>

          • <legend id='1Yu0s'><style id='1Yu0s'><dir id='1Yu0s'><q id='1Yu0s'></q></dir></style></legend>
              • <bdo id='1Yu0s'></bdo><ul id='1Yu0s'></ul>
                <i id='1Yu0s'><tr id='1Yu0s'><dt id='1Yu0s'><q id='1Yu0s'><span id='1Yu0s'><b id='1Yu0s'><form id='1Yu0s'><ins id='1Yu0s'></ins><ul id='1Yu0s'></ul><sub id='1Yu0s'></sub></form><legend id='1Yu0s'></legend><bdo id='1Yu0s'><pre id='1Yu0s'><center id='1Yu0s'></center></pre></bdo></b><th id='1Yu0s'></th></span></q></dt></tr></i><div id='1Yu0s'><tfoot id='1Yu0s'></tfoot><dl id='1Yu0s'><fieldset id='1Yu0s'></fieldset></dl></div>

                <small id='1Yu0s'></small><noframes id='1Yu0s'>

                1. 本文介绍了window.open 在 chrome 扩展中返回 undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有基于内容脚本的 Chrome 扩展程序.我通过内容脚本中的弹出窗口启动登录过程.

                  I have content script based Chrome extension. I initiate the sign in process through a popup window in the content script.

                  我使用下面的代码打开一个弹出窗口,然后等待它关闭.

                  I open a popup window using the below code and then wait till its closed.

                  但是,我从 window.open 方法中得到一个未定义".有人知道为什么会这样吗?

                  However, I get an 'undefined' from window.open method. Does anybody know why this happens?

                  loginwin 在下面的代码中是 undefined,尽管弹出窗口打开时使用指定的 login_url 很好.下面的代码是从我的内容脚本中调用的.

                  loginwin is undefined in below code although the popup window opens up fine with the specified login_url. The code below is called from my content script.

                  var loginWin = window.open(login_url, 'LoginWindow', "width=655,height=490");
                  console.log(loginWin);
                  // Check every 100 ms if the popup is closed.
                  var finishedInterval = setInterval(function() {
                      console.log('checking if loginWin closed');
                      if (loginWin.closed) {
                          clearInterval(finishedInterval);
                          console.log('popup is now closed');
                          Backbone.history.navigate('index', true);
                      }
                  }, 1000);
                  

                  推荐答案

                  注意:此答案已过时.Chrome 扩展中的 window.open() 总是返回 null (当弹出窗口被阻止时)或 window 对象.以下信息仅适用于非常旧的 (2012) 版本的 Chrome.

                  Note: This answer is obsolete. window.open() in a Chrome extension always returns either null (when the popup is blocked) or a window object. The information below only applies to very old (2012) versions of Chrome.

                  <小时>

                  内容脚本无法访问页面的全局 window 对象.对于内容脚本,以下内容适用:


                  Content scripts do not have any access to a page's global window object. For content scripts, the following applies:

                  • window 变量不引用页面的全局对象.相反,它指的是一个新的上下文,一个页面上的层".页面的 DOM 是完全可访问的.#execution-environment

                  给定一个由   组成的文档<iframe id="frameName" src="http://domain/"></iframe>:

                  Given a document consisting of   <iframe id="frameName" src="http://domain/"></iframe>:

                  • 对框架内容的访问受到页面同源策略的限制;您的扩展程序的权限不会放宽政策.
                  • frames[0]frames['frameName'],(通常指帧的包含全局 window 对象)是 <强>未定义.
                  • var iframe = document.getElementById('frameName');
                    • iframe.contentDocument 返回包含框架的 document 对象,因为内容脚本可以访问页面的 DOM.当应用同源策略时,此属性为 null.
                    • iframe.contentDocument.defaultView(指与文档关联的window对象)是undefined.
                    • iframe.contentWindow未定义.
                    • Access to the contents of a frame is restricted by the Same origin policy of the page; the permissions of your extension does not relax the policy.
                    • frames[0] and frames['frameName'], (normally referring to the the frame's containing global window object) is undefined.
                    • var iframe = document.getElementById('frameName');
                      • iframe.contentDocument returns a document object of the containing frame, because content scripts have access to the DOM of a page. This property is null when the Same origin policy applies.
                      • iframe.contentDocument.defaultView (refers to the window object associated with the document) is undefined.
                      • iframe.contentWindow is undefined.

                      如您所见,window.open() 不返回 Window 实例(window.opener 也不返回,等等).

                      As you can see, window.open() does not return a Window instance (neither does window.opener, and so forth).

                      • 在页面中注入代码,使其在页面上下文中运行.注意:仅当您正在操作的页面可以信任时才使用此方法.要在注入脚本和内容脚本之间进行通信,您可以使用:

                      • Inject the code in the page, so that it runs in the context of the page. Note: Only use this method if the page you're operating on can be trusted. To communicate between the injected script and the content script, you could use:

                      var login_url = 'http://example.com/';
                      var event_name = 'robwuniq' + Math.random().toString(16); // Unique name
                      document.addEventListener(event_name, function localName() {
                          document.removeEventListener(event_name, localName); // Clean-up
                          // Your logic:
                          Backbone.history.navigate('index', true);
                      });
                      // Method 2b: Inject code which runs in the context of the page
                      var actualCode = '(' + function(login_url, event_name) {
                          var loginWin = window.open(login_url, 'LoginWindow', "width=655,height=490");
                          console.log(loginWin);
                          // Check every 100 ms if the popup is closed.
                          var finishedInterval = setInterval(function() {
                              console.log('checking if loginWin closed');
                              if (loginWin.closed) {
                                  clearInterval(finishedInterval);
                                  console.log('popup is now closed');
                                  // Notify content script
                                  var event = document.createEvent('Events');
                                  event.initEvent(event_name, false, false);
                                  document.dispatchEvent(event);
                              }
                          }, 1000);
                      } + ')(' + JSON.stringify(login_url+'') + ', "' + event_name + '")';
                      var script = document.createElement('script');
                      script.textContent = actualCode;
                      (document.head||document.documentElement).appendChild(script);
                      script.parentNode.removeChild(script);
                      

                    • 使用 window.open() 从后台页面启动窗口.这将返回一个 window 对象,该对象具有可靠的 closed 属性.有关通信流程的更多详细信息,请参阅下一个要点.

                    • Launch the window from the background page using window.open(). This returns a window object which has a reliable closed property. See the next bullet point for more details on the communication flow.

                      这篇关于window.open 在 chrome 扩展中返回 undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                  <small id='Tdgba'></small><noframes id='Tdgba'>

                      <tbody id='Tdgba'></tbody>
                    • <i id='Tdgba'><tr id='Tdgba'><dt id='Tdgba'><q id='Tdgba'><span id='Tdgba'><b id='Tdgba'><form id='Tdgba'><ins id='Tdgba'></ins><ul id='Tdgba'></ul><sub id='Tdgba'></sub></form><legend id='Tdgba'></legend><bdo id='Tdgba'><pre id='Tdgba'><center id='Tdgba'></center></pre></bdo></b><th id='Tdgba'></th></span></q></dt></tr></i><div id='Tdgba'><tfoot id='Tdgba'></tfoot><dl id='Tdgba'><fieldset id='Tdgba'></fieldset></dl></div>
                        <legend id='Tdgba'><style id='Tdgba'><dir id='Tdgba'><q id='Tdgba'></q></dir></style></legend>
                          <bdo id='Tdgba'></bdo><ul id='Tdgba'></ul>
                          1. <tfoot id='Tdgba'></tfoot>