[英]Service Worker TypeError when opening Chrome extension
當我打開 WAVE(Web Accessibility Evaluation Tool)擴展時,我的服務人員在 Chrome 中拋出了這個錯誤:
Uncaught (in promise) TypeError: Request scheme 'chrome-extension' is unsupported at sw.js:52 (anonymous) @ sw.js:52 Promise.then (async) (anonymous) @ sw.js:50 Promise.then (異步)(匿名)@ sw.js:45 Promise.then(異步)(匿名)@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);
}
})
);
})
])
);
});
}());
我不清楚問題的性質以及如何糾正它。 非常感謝您的幫助!
WAVE 在您的站點中包含一些代碼,然后它會向 WAVE 擴展本身發出一些請求,其 URL 以 chrome-extension://xyz 開頭。 您的服務攔截了此請求並希望自己進行提取,因為此請求未緩存。 但是 service worker 中不允許使用協議/請求方案 chrome-extension:// 的 URL。
因此,您可能不想使用 Service Worker 處理這些 WAVE 請求。 用類似的東西跳過它們
if(!(event.request.url.indexOf('http') === 0)){
//skip request
}
** 這是因為安裝了 chrome 擴展** 在我的情況下是 wappalyzer
在我的情況下,我使用 chrome 和擴展程序 wappalyzer ......我更改為讀取並將站點數據更改為“當您單擊擴展程序時”
任何或所有這些作品。
if (
url.startsWith('chrome-extension') ||
url.includes('extension') ||
!(url.indexOf('http') === 0)
) return;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.