How to intercept a web request

假如想象 提交于 2019-12-12 08:01:58

问题


Is there something like Chromium's chrome.webRequest for Safari extensions? I went through their documentation here. The closest thing I could find was SafariBeforeNavigateEvent. This will prevent the new page load but still would send the request to the server. Moreover I don't think it will call the listeners on AJAX requests. Anyone tried something similar?


回答1:


We solved this problem by using "xmlhttprequest" overriding.

This is our content.js . We injected content.js as start script

    $(document).ready(function() {

     var myScriptTag = document.createElement('script');
     myScriptTag.src = safari.extension.baseURI + 'js/injectedToPage.js';
     document.body.appendChild(myScriptTag);     

    });    

injected code is: (injectedToPage.js)

XMLHttpRequest.prototype.reallySend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function (body) { 

        console.log("--req.body:---");
        console.log(body);

    this.reallySend(body);

};
var req = new XMLHttpRequest();
req.open("GET", "any.html", true);
req.send(null);


来源:https://stackoverflow.com/questions/20937287/how-to-intercept-a-web-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!