打开Chrome扩展程序时,Service Worker TypeError

时间:2018-03-07 17:11:49

标签: javascript google-chrome google-chrome-extension promise service-worker

当我打开WAVE(Web辅助功能评估工具)扩展程序时,我的服务工作者在Chrome中抛出此错误:

  

未捕获(承诺)TypeError:请求方案'chrome-extension'是   不支持       在sw.js:52(匿名)@ sw.js:52 Promise.then(async)(匿名)@ sw.js:50 Promise.then(async)(匿名)@ sw.js:45   Promise.then(async)(匿名)@ 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);
                            }
                        })
                    );
                })
            ])
        );
    });
}());

我不清楚问题的性质以及如何纠正它。非常感谢您的帮助!

3 个答案:

答案 0 :(得分:6)

WAVE在您的网站中包含一些代码,然后使用以chrome-extension:// xyz开头的网址向WAVE扩展本身发出一些请求。您的服务拦截此请求并希望自己进行提取,因为此请求未被缓存。但是在服务工作者中不允许使用协议/请求方案chrome-extension://的URL。

因此,您可能不希望与服务工作者一起处理这些WAVE请求。用

之类的东西略过它们
if(!(event.request.url.indexOf('http') === 0)){
   //skip request
}

答案 1 :(得分:0)

**这是因为安装了chrome扩展程序** 就我而言,是wappalyzer

答案 2 :(得分:0)

就我而言我使用 chrome 并扩展 wappalyzer ... 我更改为读取并将站点数据更改为“当您单击扩展程序时”