打开Chrome扩展程序时出现Service Worker TypeError

f87krz0w  于 2023-11-14  发布在  Go
关注(0)|答案(5)|浏览(103)

当我打开WAVE(Web Accessibility Evaluation Tool)扩展时,我的服务工作人员在Chrome中抛出此错误:
Uncaught(in promise)TypeError:请求方案'chrome-extension'在sw.js:52(anonymous)@ sw. js:52 Promise.then(promise)(anonymous)@ sw.js:50 Promise.then(promise)(anonymous)@ sw.js:45 Promise.then(promise)(anonymous)@ sw.js:38不受支持
我的服务员代码是:

(function () {
    'use strict';
    var consoleLog;
    var writeToConsole;
    const CACHE_NAME = '20180307110051';
    const CACHE_FILES = [
        'https://fonts.gstatic.com/s/notosans/v6/9Z3uUWMRR7crzm1TjRicDv79_ZuUxCigM2DespTnFaw.woff2',
        'https://fonts.gstatic.com/s/notosans/v6/ByLA_FLEa-16SpQuTcQn4Igp9Q8gbYrhqGlRav_IXfk.woff2',
        'https://fonts.gstatic.com/s/notosans/v6/LeFlHvsZjXu2c3ZRgBq9nJBw1xU1rKptJj_0jans920.woff2',
        'https://fonts.gstatic.com/s/notosans/v6/PIbvSEyHEdL91QLOQRnZ1xampu5_7CjHW5spxoeN3Vs.woff2',
        'https://fonts.gstatic.com/s/materialicons/v22/2fcrYFNaTjcS6g4U3t-Y5ZjZjT5FdEJ140U2DJYC3mY.woff2',
        'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/fonts/fontawesome-webfont.woff2',
        'favicon.20180205072319.ico',
        'favicons/android-chrome-512x512.20180211120531.png',
        'favicons/android-chrome-192x192.20180211120531.png',
        'offline.html'
    ];
// for debugging:
    writeToConsole = false;
    consoleLog = function (message) {
        if (writeToConsole) {
            console.log(message);
        }
    };
// https://stackoverflow.com/questions/37117933/service-workers-not-updating
    self.addEventListener('install', function (e) {
        e.waitUntil(
            Promise.all([caches.open(CACHE_NAME), self.skipWaiting()]).then(function (storage) {
                var static_cache = storage[0];
                return Promise.all([static_cache.addAll(CACHE_FILES)]);
            })
        );
    });
// intercept network requests:
    self.addEventListener('fetch', function (event) {
        consoleLog('Fetch event for ' + event.request.url);
        event.respondWith(
            caches.match(event.request).then(function (response) {
                if (response) {
                    consoleLog('Found ' + event.request.url + ' in cache');
                    return response;
                }
                consoleLog('Network request for ' + event.request.url);
// add fetched files to the cache:
                return fetch(event.request.clone()).then(function (response) {
// Respond with custom 404 page
                    if (response.status === 404) {
                        return caches.match('error?status=404');
                    }
                    return caches.open(CACHE_NAME).then(function (cache) {
                        if (event.request.url.indexOf('test') < 0) {
                            cache.put(event.request.url, response.clone());
                        }
                        return response;
                    }).catch(function () {
                        console.log("Uncaught (in promise) TypeError: Request scheme 'chrome-extension' is unsupported");
                    });
                });
            }).catch(function (error) {
// respond with custom offline page:
                consoleLog('Error, ' + error);
// Really need an offline page here:
                return caches.match('offline.html');
            })
        );
    });
// delete unused caches
// https://stackoverflow.com/questions/37117933/service-workers-not-updating
    self.addEventListener('activate', function (e) {
        e.waitUntil(
            Promise.all([
                self.clients.claim(),
                caches.keys().then(function (cacheNames) {
                    return Promise.all(
                        cacheNames.map(function (cacheName) {
                            if (cacheName !== CACHE_NAME) {
                                console.log('deleting', cacheName);
                                return caches.delete(cacheName);
                            }
                        })
                    );
                })
            ])
        );
    });
}());

字符串
我不清楚问题的性质和如何纠正它。非常感谢提前帮助!

8ehkhllq

8ehkhllq1#

WAVE在您的站点中包含一些代码,然后它会使用以chrome-extension://xyz开头的URL向WAVE扩展本身发出一些请求。您的服务拦截了此请求,并希望自己获取,因为此请求未被缓存。但是service worker中不允许使用协议/请求方案chrome-extension://的URL。
因此,您可能不希望使用服务工作者处理这些WAVE请求。

if(!event.request.url.startsWith('http')){
   //skip request
}

字符串

ohfgkhjo

ohfgkhjo2#

这是因为安装了Chrome扩展在我的情况下,它是wappalyzer

5lwkijsr

5lwkijsr3#

这些作品中的任何一件或全部

if (
    url.startsWith('chrome-extension') ||
    url.includes('extension') ||
    !(url.indexOf('http') === 0)
) return;

字符串
但我在想'一个请求可以来自否定上述条件的扩展吗?'如果是,那么我如何确保唯一成功的是来自'self'的请求。

zqry0prt

zqry0prt4#

  • 在我的情况下 * 我使用Chrome和扩展wappalyzer.我改变阅读和更改网站数据“当你点击扩展程序”
gmxoilav

gmxoilav5#

如果您正在使用HTTP响应

if(!event.request.url.startsWith('http')){
        event.waitUntil(addToCache(event.request));
     }
     and if you are on https response

     if(!event.request.url.startsWith('https')){
        event.waitUntil(addToCache(event.request));
     }

字符串

相关问题