Block Service Workers

Blocks Service Worker's registration.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Block Service Workers
// @name:zh-CN  禁用Service Workers
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @grant       GM_addElement
// @grant       GM_info
// @version     0.7
// @grant       GM_log
// @author      axototl
// @license     Unlicense
// @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';

const main = async (logger) => {
    'use strict';
    if (!('serviceWorker' in navigator)) {
        console.warn("Did not detected SW APIs, exiting...");
        return;
    }
    logger("Removing Service Workers API, please wait...");
    navigator.serviceWorker.register =
        () => new Promise((res, rej) => rej("This method is not allowed!"));
    logger("Deregistering Installed Service Workers, please wait...");
    (await navigator.serviceWorker.getRegistrations())
        .forEach(it => it.unregister());
    logger("All done!");
}

if (GM_info.injectInto === 'page' || GM_info.sandboxMode === "raw") main(GM_log);
else {
    const blobURL = URL.createObjectURL(new Blob([`(${main.toString()})(console.log);`], {type: "text/javascript"}));
    GM_addElement("script", {src: blobURL}).onload =
        () => setTimeout(() => (URL.revokeObjectURL(blobURL), GM_log("Destroyed")), 1000);
}