• <small id='ZLA0S'></small><noframes id='ZLA0S'>

    1. <tfoot id='ZLA0S'></tfoot>

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

        为什么这段代码不起作用?我正在创建一个 Firef

        时间:2023-10-14
          <tbody id='84j6Y'></tbody>

            • <tfoot id='84j6Y'></tfoot>

              <small id='84j6Y'></small><noframes id='84j6Y'>

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

                  本文介绍了为什么这段代码不起作用?我正在创建一个 Firefox 扩展,但代码没有运行.但是,如果我将代码粘贴到控制台中,它就可以工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我做了一个 Firefox 扩展来获取所有请求的 url 并显示它们.但代码只有在我将其粘贴到控制台时才有效.

                  I make a firefox extension that get all the request url's and displays them. But the code only works if I paste it in the console.

                  当扩展程序加载时它没有显示任何错误,它似乎只是不会运行

                  when the extension loads it doesn't show any error, it seems like it just won't run

                  这是完整的代码

                  xhrScript.js

                  (function(){
                  
                      const proxiedOpen = XMLHttpRequest.prototype.open;
                      window.XMLHttpRequest.prototype.open = function ( _, url) {
                          this.__URL = url;
                          return proxiedOpen.apply(this, arguments);
                      };
                  
                      const proxiedSend = window.XMLHttpRequest.prototype.send;
                      window.XMLHttpRequest.prototype.send = function () {
                          const { protocol, host } = window.location;
                          // showing only when it paste in console
                          console.log("full request url ", `${protocol}//${host}${this.__URL}`);
                          return proxiedSend.apply(this, [].slice.call(arguments));
                      };
                  
                  })();
                  
                  // this works all times
                  document.body.style.border = "7px solid blue";
                  

                  ma​​nifest.json

                  {
                      "manifest_version": 2,
                      "name": "XHR request urls",
                      "version": "1.0",
                      "description": "get all the request url's",
                  
                      "content_scripts": [
                        {
                          "matches": ["*://*/*"],
                          "js": ["xhrScript.js"]
                        }
                      ]  
                  }
                  

                  如您所见,最后一行是 document.body.style.border = "7px solid blue";,每次都可以正常工作.但是 XMLHttpRequest opensend 方法不起作用.仅当我将代码粘贴到控制台时才有效.

                  As you can see, in the last line is document.body.style.border = "7px solid blue";, this works fine every time. But the XMLHttpRequest open and send methods don't work. only works if I paste the code in the console.

                  如果您想查看示例,可以尝试将 xhrScript.js 代码复制并粘贴到 https://reactjs.org(这是一个 SPA,所以很容易检查我想要什么)在 devTools 控制台中,并查看所有请求.

                  if you want see an example, you can try copy and paste the xhrScript.js code in https://reactjs.org (it's a SPA, so it's easy to check what I want) in the devTools console, and see all the request.

                  我不知道为什么这段代码只有在控制台粘贴时才会运行

                  I don't know why this code only runs when it is pasted in console

                  推荐答案

                  内容脚本在隔离的 JavaScript 环境中运行,这意味着 window 及其内容与页面隔离,因此当您修改它时,您只修改内容脚本的版本.

                  Content scripts run in an isolated JavaScript environment meaning that window and its contents are isolated from the page so when you modify it, you only modify the content script's version.

                  有两种解决方案:

                  1. Firefox 专用.

                  1. Firefox-specific.

                  使用 wrappedJSObjectexportFunction 访问页面上下文(更多信息):

                  Use wrappedJSObject and exportFunction to access the page context (more info):

                  const urls = new WeakMap();
                  const origXhr = hookPagePrototype('XMLHttpRequest', {
                    open(method, url) {
                      urls.set(this, url);
                      return origXhr.open.apply(this, arguments);
                    },
                    send() {
                      console.log('Sending', new URL(urls.get(this), location).href);
                      return origXhr.send.apply(this, arguments);
                    },
                  });
                  
                  function hookPagePrototype(protoName, funcs) {
                    const proto = wrappedJSObject[protoName].prototype;
                    const oldFuncs = {};
                    for (const [name, fn] of Object.entries(funcs)) {
                      oldFuncs[name] = exportFunction(proto[name], wrappedJSObject);
                      proto[name] = exportFunction(fn, wrappedJSObject);
                    }
                    return oldFuncs;
                  }
                  

                1. Chrome 兼容.

                2. Chrome-compatible.

                  使用 DOM 脚本在页面上下文中运行代码:说明.

                  它不适用于受严格的 Content-Security-Policy (CSP) 保护的页面,该 CSP 会阻止脚本执行,因此在为 Firefox 编写扩展时,我们应该改用 wrappedJSObject 方法.

                  It won't work on pages protected by a strict Content-Security-Policy (CSP) that prevents script execution so when writing an extension for Firefox we should use wrappedJSObject method instead.

                  这篇关于为什么这段代码不起作用?我正在创建一个 Firefox 扩展,但代码没有运行.但是,如果我将代码粘贴到控制台中,它就可以工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                3. 上一篇:跨域请求本地文件 下一篇:XHR responseText 是空字符串

                  相关文章

                  最新文章

                    <bdo id='5Q7Wz'></bdo><ul id='5Q7Wz'></ul>
                4. <i id='5Q7Wz'><tr id='5Q7Wz'><dt id='5Q7Wz'><q id='5Q7Wz'><span id='5Q7Wz'><b id='5Q7Wz'><form id='5Q7Wz'><ins id='5Q7Wz'></ins><ul id='5Q7Wz'></ul><sub id='5Q7Wz'></sub></form><legend id='5Q7Wz'></legend><bdo id='5Q7Wz'><pre id='5Q7Wz'><center id='5Q7Wz'></center></pre></bdo></b><th id='5Q7Wz'></th></span></q></dt></tr></i><div id='5Q7Wz'><tfoot id='5Q7Wz'></tfoot><dl id='5Q7Wz'><fieldset id='5Q7Wz'></fieldset></dl></div>
                5. <tfoot id='5Q7Wz'></tfoot>

                    <legend id='5Q7Wz'><style id='5Q7Wz'><dir id='5Q7Wz'><q id='5Q7Wz'></q></dir></style></legend>

                    <small id='5Q7Wz'></small><noframes id='5Q7Wz'>