简体   繁体   English

覆盖 Chrome 扩展的 XMLHttpRequest 原型

[英]Overridding XMLHttpRequest Prototype For Chrome Extension

I am trying to intercept all XMLHttpRequests with my extension and I can currently do it to some extent but it does not work fully.我试图用我的扩展程序拦截所有 XMLHttpRequests,我目前可以在一定程度上做到这一点,但它不能完全工作。 I know about webRequests but it does not let me edit the post/get data unlike with the current code below.我知道 webRequests 但它不允许我编辑 post/get 数据,这与下面的当前代码不同。 I can run this code我可以运行这段代码

Inject.js注入.js

(function(){
    console.log("Injected Send")
    var proxiedSend = window.XMLHttpRequest.prototype.send;
    window.XMLHttpRequest.prototype.send = function() {
        console.log(this, arguments );
        return proxiedSend.apply(this, [].slice.call(arguments));
    }
})();
(function(){
    console.log("Injected Open")
    var proxiedOpen = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function() {
        console.log(this, arguments );
        return proxiedOpen.apply(this, [].slice.call(arguments));
    };
})();

Inside my content script and it will log the "Injected Send" and "Injected Open".在我的内容脚本中,它将记录“Injected Send”和“Injected Open”。 I have tested this code and it behaves weirdly.我已经测试了这段代码,它的行为很奇怪。

If you run this code below in the console you would see that it works at intercepting the request but it won't work on any XMLHttpRequests running on the page (old and new objects).如果您在控制台中运行下面的这段代码,您会看到它可以拦截请求,但不适用于页面上运行的任何 XMLHttpRequests(旧对象和新对象)。 It will only intercept XMLHttpRequests made in the console.它只会拦截在控制台中生成的 XMLHttpRequests。 Another example is this https://www.w3schools.com/code/tryit.asp?filename=GBUKE1JS7IFL .另一个例子是这个https://www.w3schools.com/code/tryit.asp?filename=GBUKE1JS7IFL It works in the webpage but I can't get the same behavior by injecting it with an extension.它在网页中工作,但我无法通过向它注入扩展来获得相同的行为。

(function(){
    console.log("Injected Send")
    var proxiedSend = window.XMLHttpRequest.prototype.send;
    window.XMLHttpRequest.prototype.send = function() {
        console.log(this, arguments );
        return proxiedSend.apply(this, [].slice.call(arguments));
    }
})();
(function(){
    console.log("Injected Open")
    var proxiedOpen = window.XMLHttpRequest.prototype.open;
    window.XMLHttpRequest.prototype.open = function() {
        console.log(this, arguments );
        return proxiedOpen.apply(this, [].slice.call(arguments));
    };
})();

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log("success")
    }
};
xhttp.open("GET", "/", true);
xhttp.send();

This problem is causing my Inject.js to not work.这个问题导致我的Inject.js无法工作。

manifest.json清单文件

{
  "name": "test",
  "version": "0.0.1",
  "manifest_version": 2,
  "description": "testing xmlhttprequests",
  "homepage_url": "http://example.com",
  "icons": {
    "16": "icons/icon16.png",
    "48": "icons/icon48.png",
    "128": "icons/icon128.png"
  },
  "default_locale": "en",
  "background": {
    "scripts": [
      "src/bg/background.js"
    ],
    "persistent": true  
  },
  "page_action": {
    "default_icon": "icons/icon19.png",
    "default_title": "page action demo",
    "default_popup": "src/page_action/page_action.html"
  },
  "permissions": [
    "tabs",
    "*://*.google.com/"
  ],
  "content_scripts": [
    {
      "matches": [
        "https://*/*",
        "http://*/*"
      ],
      "run_at": "document_start",
      "js": [
        "src/inject/inject.js"
      ]
    }
  ]
}

You need to run it in a DOM script, see Insert code into the page context using a content script .您需要在 DOM 脚本中运行它,请参阅使用内容脚本将代码插入页面上下文

-wOxxOm -wOxxOm

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

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