sw

sw.js

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/473118/1235563/sw.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         测试js
// @namespace
// @include      *
// @version      0.0.1
// @description  测试专用js
// @author       ymzhao
// ==/UserScript==
(function () {
    // this
    this.addEventListener('install', function (event) {
      console.log('Service Worker install');
    });
    self.addEventListener('install', event => {
      // 安装中
      
      event.waitUntil(
        // 可以预缓存重要的和长期的资源
      );
    });
    
    self.addEventListener('activate', event => {
      // 已激活,成功控制客户端
      
      // 如果activate足够快,clients.claim的调用在请求之前,就能保证第一次请求页面也能拦截请求
      clients.claim();
      
      event.waitUntil(
        // 可以在这里清除旧的缓存
      );
    });
    
    self.addEventListener('fetch', event => {
      const url = new URL(event.request.url);
      // 拦截/api/user接口的请求,返回自定义的response
      if (url.pathname == '/api/user') {
        event.respondWith(
          // response body只能接受String, FormData, Blob等类型,所以这里序列化
          new Response(JSON.stringify({ name: 'Javen' }), {
            headers: {
              'Content-Type': 'application/json',
            },
          })
        );
      }
    });
})