【问题标题】:Removing images from a page before they load在加载之前从页面中删除图像
【发布时间】:2015-09-11 23:13:01
【问题描述】:
我正在使用 Crossrider 开发一个扩展程序,并希望在加载某些图像之前阻止它们。目前,我正在使用下面的代码在 extension.js 文件中尝试,但它只会在它们加载后删除它们并且不会捕获 AJAX 加载的图像。如何使用 Crossrider 做到这一点?
appAPI.ready(function($) {
$('img').remove();
});
【问题讨论】:
标签:
image
cross-browser
loading
crossrider
【解决方案1】:
这最好在后台范围内使用appAPI.webRequest.onRequest.addListener 方法来捕获图像请求并在它们加载之前阻止它们。例如:
appAPI.ready(function() {
// The list of image file types you wish to block
var fileTypeBlockList = 'jpg|gif|svg';
appAPI.webRequest.onRequest.addListener(function(details) {
if (details.method == "GET" &&
details.requestUrl.match(new RegExp('.'+fileTypeBlockList+'$','i')) {
return { cancel: true };
}
});
});
[披露:我是 Crossrider 的员工]