禁用Service Workers

阻止Service Worker注册,并移除现有的Service Workers,杜绝网上垃圾。

目前为 2023-10-22 提交的版本。查看 最新版本

// ==UserScript==
// @name        Block Service Workers
// @name:zh-CN  禁用Service Workers
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @unwrap
// @version     0.3.7
// @author      axototl
// @inject-into page
// @sandbox     JavaScript
// @license     AGPL-3.0-or-later
// @description Blocks Service Worker's registration.
// @description:zh-CN 阻止Service Worker注册,并移除现有的Service Workers,杜绝网上垃圾。
// @icon        https://www.w3.org/favicon.ico
// @run-at      document-start
// ==/UserScript==
'use strict';

(async () => {
    console.groupCollapsed("remove SW APIs");
    if (!('serviceWorker' in navigator)) {
        console.warn("Did not detected SW APIs, exiting...");
        console.groupEnd();
        return;
    }
    console.log("Removing Service Workers API, please wait...");
    navigator.serviceWorker.register = () => new Promise((res, rej) => rej("This method is not allowed!"));
    console.log("Deregistering Installed Service Workers, please wait...");

    const arrs = await navigator.serviceWorker.getRegistrations();
    for (const it of arrs) it.unregister();

    console.log("All done!");
    console.groupEnd();
})();