You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展匹配规则技术求助:如何批量阻止Pinterest全顶级域名

解决Chrome扩展中批量阻止Pinterest全域名的问题

首先,我注意到你代码里的一个关键错误:你把多个匹配规则塞进了同一个字符串里,而不是作为数组的独立元素。Chrome的urls参数要求每个匹配模式都是数组的单独项,用逗号分隔在单个字符串里是完全不生效的。

另外,针对批量匹配Pinterest所有顶级域名的需求,Chrome的匹配模式支持更灵活的写法,根本不用逐个枚举域名。这里给你两个核心匹配规则:

  • 要匹配所有以pinterest为主域名的地址(比如pinterest.com、pinterest.se),用*://pinterest.*/*
  • 要匹配所有包含pinterest的子域名(比如www.pinterest.com、blog.pinterest.se),用*://*.pinterest.*/*

如果想同时覆盖主域名和各类子域名,把这两个模式都加入数组就可以了。

下面是修正后的完整代码:

var blockedUrls = [
    "*://pinterest.*/*",
    "*://*.pinterest.*/*"
];
var host = "https://www.google.com";

chrome.webRequest.onBeforeRequest.addListener(
    function(details) { return {redirectUrl: host }; },
    { 
        urls: blockedUrls,
        types: ["main_frame", "sub_frame", "stylesheet", "script", "image", "object", "xmlhttprequest", "other"] 
    },
    ["blocking"]
);

补充说明:

  • Chrome匹配模式里,*在主机名部分可以匹配任意长度的字符(包括空字符),所以pinterest.*甚至能匹配像pinterest.co.uk这类多级后缀的域名
  • 别忘了在你的扩展manifest.json里声明webRequest和webRequestBlocking权限,同时确保权限范围覆盖到这些目标域名(或者直接用<all_urls>简化配置)

内容的提问来源于stack exchange,提问作者CopyPasteThankYou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:32:06