ReplaceGoogleCDN扩展 V2版本和V3版本的区别

文档说明:只记录关键地方; 2023-02-11

环境: chromium 内核

备注: firefox 版本113 开始支持 declarativeNetRequest

区别:

  1. v2 版本允许通过 javascript 脚本动态修改 HTTP 请求头、请求体、响应头、响应体
  2. v3 版本为了安全,不允许 javascript 脚本动态修改 HTTP 请求头、请求体、响应性头、响应体
  3. v3 版本允许修改动作, 变更为声明式修改规则,不允许脚本动态修改

共同点:

  1. 浏览器URL地址重定向工具 (例子: developers.google.com 自动重定向到 developers.google.cn)

核心关键点函数区别

v2版本核心函数

编写js 脚本就能修改 http 请求、响应内容

/**
 * 响应头里CSP相关的选项
 * @type {string[]}
 */

const remove_csp_item = [
  "content-security-policy",
  "content-security-policy-report-only",
  "expect-ct",
  "report-to",
  "x-content-security-policy",
  "x-webkit-csp",
  "x-xss-protection",
  "x-permitted-cross-domain-policies",
  "x-content-type-options",
  "x-frame-options",
  "permissions-policy",
  "timing-allow-origin",
  "cross-origin-embedder-policy",
  "cross-origin-opener-policy",
  "cross-origin-opener-policy-report-only",
  "cross-origin-embedder-policy-report-only",
];

/**
 * 需要移除CSP的URL
 * @type {string[]}
 */
const remove_csp_urls = [
  "*://ajax.googleapis.com/*",
  "*://fonts.googleapis.com/*",
  "*://themes.googleusercontent.com/*",
  "*://fonts.gstatic.com/*",
  "*://*.google.com/*",
  "*://secure.gravatar.com/*",
  "*://www.gravatar.com/*",
  "*://maxcdn.bootstrapcdn.com/*",
  "*://api.github.com/*",
  "*://www.gstatic.com/*",
  "*://stackoverflow.com/*",
  "*://translate.googleapis.com/*",
  "*://developers.redhat.com/*",
  "*://*.githubusercontent.com/*",
  "*://pub.dev/*",
  "*://stackoverflow.com/*",
];

/**
 * 移除 Content-Security-Policy
 */

chrome.webRequest.onHeadersReceived.addListener(
    (details) => {
      //移除响应头 Content-Security-Policy
      details.responseHeaders = details.responseHeaders.filter(
          (response_header) =>
              !remove_csp_item.includes(response_header.name.toLowerCase())
      );
      return {
        responseHeaders: details.responseHeaders,
      };
    },
    {
      //    urls: ["<all_urls>"],
      urls: [
        ...remove_csp_urls, //需要移除 Content-Security-Policy 的URL地址
      ],
      types: [
        "main_frame",
        "sub_frame",
        "stylesheet",
        "script",
        "image",
        "font",
        "object",
        "xmlhttprequest",
        "ping",
        "other",
      ],
    },
    ["blocking", "responseHeaders"]
);

/*
  请求地址重定向
  1. https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/onBeforeRequest
*/
chrome.webRequest.onBeforeRequest.addListener(
    function (details) {
      let url = details.url.replace("http://", "https://");
      url = url.replace("ajax.googleapis.com", "ajax.loli.net");
      //url = url.replace("fonts.googleapis.com", "fonts.loli.net");
      url = url.replace("fonts.googleapis.com", "fonts.googleapis.cn");
      url = url.replace("themes.googleusercontent.com", "themes.loli.net");
      //url = url.replace("fonts.gstatic.com", "gstatic.loli.net");
      url = url.replace("fonts.gstatic.com", "fonts.gstatic.cn");
      url = url.replace(
          "www.google.com/recaptcha/",
          "www.recaptcha.net/recaptcha/"
      );
      url = url.replace("secure.gravatar.com", "gravatar.loli.net");
      url = url.replace("www.gravatar.com", "gravatar.loli.net");
      url = url.replace("en.gravatar.com", "gravatar.loli.net");
      url = url.replace("cn.gravatar.com", "gravatar.loli.net");

      url = url.replace("cdn.jsdelivr.net", "fastly.jsdelivr.net");
      //"cdn.bootcdn.net/ajax/libs/twitter-bootstrap/"
      //"cdn.jsdelivr.net/npm/bootstrap@$1/dist/$2"
      url = url.replace(
          /maxcdn\.bootstrapcdn\.com\/bootstrap\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
          "lib.baomitu.com/twitter-bootstrap/$1/$2"
      );
      url = url.replace(
          /code\.jquery\.com\/jquery-(\d{1,4}\.\d{1,4}\.\d{1,4})(.*?)/g,
          "lib.baomitu.com/jquery/$1/jquery$2"
      );
      url = url.replace(
          /code\.jquery\.com\/ui\/(\d{1,4}\.\d{1,4}\.\d{1,4})\/(.*?)/g,
          "ajax.aspnetcdn.com/ajax/jquery.ui/$1/$2"
      );
      url = url.replace("developers.google.com", "developers.google.cn");
      url = url.replace("developer.android.com", "developer.android.google.cn");
      url = url.replace("source.android.com", "source.android.google.cn");
      url = url.replace("www.gstatic.com", "www.gstatic.cn");
      return { redirectUrl: url };
    },
    {
      urls: [
        "*://ajax.googleapis.com/*",
        "*://fonts.googleapis.com/*",
        "*://themes.googleusercontent.com/*",
        "*://fonts.gstatic.com/*",
        "*://www.google.com/recaptcha/*",
        "*://secure.gravatar.com/*",
        "*://en.gravatar.com/*",
        "*://cn.gravatar.com/*",
        "*://www.gravatar.com/*",
        "*://maxcdn.bootstrapcdn.com/bootstrap/*",
        "*://cdn.jsdelivr.net/*",
        "*://code.jquery.com/jquery-*",
        "*://code.jquery.com/ui/*",
        "*://developers.google.com/*",
        "*://developer.android.com/*",
        "*://source.android.com/*",
        "*://www.gstatic.com/*",
      ],
    },
    ["blocking"]
);


v3版本 核心函数

只能预先申明规则,不能使用js 动态修改,规则都是: 声明式的
例子: developers.google.com 重定向到 developers.google.cn

chrome.declarativeNetRequest.updateDynamicRules(
  {
    addRules: [
      {
        id: 10,
        priority: 1,
        action: {
          type: "redirect",
          redirect: {
            transform: {
              scheme: "https",
              host: "developers.google.cn",
            },
          },
        },
        condition: {
          urlFilter: "developers.google.com",
          requestDomains: ["developers.google.com"],
          resourceTypes: [
            "main_frame",
            "sub_frame",
            "stylesheet",
            "script",
            "image",
            "font",
            "object",
            "xmlhttprequest",
            "ping",
            "csp_report",
            "media",
            "websocket",
            "webtransport",
            "webbundle",
            "other",
          ],
        },
      },
    ],
    removeRuleIds: [123],
  },
  (parameter) => {
    console.log(parameter);
  }
);

参考文档

  1. v2 启用同时支持 chromium 和 firefox
  2. v3 功能
  3. firefox 109版本开始 支持 V3版本,暂不支持eclarativeNetRequest
  4. 扩展使用自定义规则,看这里
  5. ReplaceGoogleCDN扩展下载和安装
posted @ 2023-02-11 23:33  jingjingxyk  阅读(363)  评论(0编辑  收藏  举报