Chrome扩展程序:访问前阻止页面项目 [英] Chrome extension: Block page items before access

查看:102
本文介绍了Chrome扩展程序:访问前阻止页面项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在网页上阻止项目,但是我想在加载之前这样做。因此,例如,我可以使用

chrome.webRequest.onBeforeRequest.addListener(...);



并重定向/取消请求。但我想检查请求的实际内容。我现在正在做的是开始一个 XMLHttpRequest 来自己加载url /对象,检查内容并在必要时将其阻止。
但是,主要的问题是,事实上,没有太多的对象被阻止。这意味着,每个对象都被加载了两次:一次用于我的检查,一次之后,我说好吧,你可以加载它。



如何拦截加载过程,以便我可以即时检查它并传递数据字节(如果允许)?

希望您理解我的问题,谢谢: - )



现在我怎么做的例子:

  function shall_be_blocked( info){
var xhr = new XMLHttpRequest();
xhr.open(GET,file,false);
// ...#加载文件并检查字节
if(xhr.responseText ==block it){
return true;
}
返回false;


chrome.webRequest.onBeforeRequest.addListener(
function(info){
ret = shall_be_blocked(info);
if(ret .. 。){return {cancel:true};} //加载文件一次,因为它被阻止
return {}; //加载文件两次
},
{}, [阻止]
);


解决方案

我能够实现我想要做的。该解决方案不一定需要扩展,但设置代理可能会有用。之后,解决方案(针对上述指定的问题)如下所示:


  • 在这种情况下,使用chrome扩展来使用代理, localhost

  • 设置代理脚本,例如使用python来路由所有流量(并且可能安装证书,因此也可以分析HTTP流量)
  • =>建立中间人,分析流量并在需要时进行修改



是的,这并不是一个真正的解决方案,但是它不可能实现请参阅 https://bugs.chromium.org/p/chromium/issues / detail?id = 104058 )。



最好的问候,
mutilis


I am trying to do block items on a webpage but I want to do that, before they are loaded. So, e.g., I could use

chrome.webRequest.onBeforeRequest.addListener(...);

And redirect/cancel the request. But i want to inspect the actual content of the request. What I am doing right now, is starting a XMLHttpRequest to load the url/object myself, inspect the content, and block it if necessary. However, the main problem is that in fact, not many objects are blocked. This means, that each object is loaded twice: Once for "my inspection" and once, after I said "okay, you may load it".

How can I intercept the loading process, so that I can inspect it on the fly and pass on the data bytes if they are allowed?

Hope you understand my question, thanks :-)

Example of how I do it right now:

function shall_be_blocked(info){
  var xhr = new XMLHttpRequest();
  xhr.open("GET", file, false);
  //... #load the file and inspect the bytes
  if (xhr.responseText=="block it") {
     return true;
  }
  return false;
}

chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    ret = shall_be_blocked(info);
    if (ret ...){return {cancel:true};}//loads the file once, as it is getting blocked
    return {};//loads the file twice
  },
  {},["blocking"]
);

解决方案

I was able to achieve what i was trying to do. The solution does not necessarily need extensions anymore, but to setup a proxy this might be useful. Afterwards, the solution (for the specified problem above) is as follows:

  • Use a chrome extension to use a proxy, in this case, localhost
  • Setup a proxyscript, e.g. using python, to route all traffic (and maybe install certificates, so HTTPs traffic may be analyzed as well)
  • => Man-in-the-middle established, analyze traffic and modify if needed

Yes, this is not really a solution to the problem of making a chrome extension do this somehow, but it is not possible yet (see https://bugs.chromium.org/p/chromium/issues/detail?id=104058).

Best regards, mutilis

这篇关于Chrome扩展程序:访问前阻止页面项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆