简体   繁体   English

我如何防止加载 <img> 通过内容脚本?

[英]How can I prevent loading from <img> via content script?

I am developing a Chrome Extension for GMail. 我正在为GMail开发Chrome扩展程序。 When I open a sent mail, if an image is attached, it is downloaded from server. 当我打开已发送的邮件时,如果附加了图像,则会从服务器下载图像。 I want to prevent browser hitting GET /url . 我想防止浏览器点击GET / url

I have tried - 我努力了 -

document.addEventListener("DOMSubtreeModified",function(){
    $("img-selector").remove();
},true);

I have also used mutation observer. 我还使用了变异观察器。 Both methods remove the <img> from DOM, but I can still see browser hitting that image url in Network panel. 两种方法都从DOM中删除了<img>,但是我仍然可以看到浏览器在“网络”面板中击中了该图像URL。

So, how can I prevent browser hitting the url, and removing the img? 因此,如何防止浏览器访问url并删除img? It needs to be done in Content Script. 它需要在Content Script中完成。 You can assume JQuery library is available. 您可以假定JQuery库可用。

Thanks in advance. 提前致谢。

If you know the URL patterns in advance, I suggest to use the chrome.webRequest or chrome.declarativeWebRequest APIs to block the image. 如果您事先知道网址格式,建议您使用chrome.webRequestchrome.declarativeWebRequest API来阻止图片。 This can be done from the background / event page. 这可以从后台/事件页面完成。

( this first revision of this answer showed an example of the "beforeload" event. However this event has been removed from Chrome 35, so I've deleted the example.) 这个答案的这第一个版本显示“beforeload”事件的一个例子。但这一事件已被删除 ,从Chrome 35版,所以我已经删除了的例子。)

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

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