我一直在努力将这个manifest v2 Chrome扩展移植到manifest v3,虽然我确实通过改变manifest文件的 struct 并将background.js
替换为service-worker.js
来移植扩展.Chrome扩展的目录 struct 似乎可以接受,should在manifest v3中工作,但我有一个持久的问题,我无法解决.我收到一条错误信息,指出...
错误:无法建立连接.接收端不存在."
"堆栈跟踪"
"service—worker. js:14(匿名函数)"
下面是我的service-worker.js
代码,把它放在上下文中.Note: source code is licensed under the Mozilla Public License, version 2.
// Listen for the extension being reloaded or re-installed
chrome.runtime.onInstalled.addListener(() => {
// Query for all open tabs
chrome.tabs.query({}, (tabs) => {
tabs.forEach((tab) => {
// Inject the content script into each open tab
chrome.tabs.registerContentScripts(tab.id, {
file: 'content-script.js'
});
});
});
});
function sendAudioMessage(actionType, audioFile) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
tabs.forEach(async (tab) => {
console.log(tab);
if (tab.url.startsWith("chrome")) return false;
try {
await chrome.tabs.sendMessage(tab.id, {
action: actionType
});
} catch (e) {
console.error(e);
console.trace();
}
});
});
}
// *** Message Handler ***
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log(message, sender, sendResponse);
switch (message.action) {
case 'playNavigationSound':
sendAudioMessage('playNavigationSound');
break;
case 'playDownloadCompleteSound':
sendAudioMessage('playDownloadCompleteSound');
break;
case 'playDownloadErrorSound':
sendAudioMessage('playDownloadErrorSound');
break;
case 'playMuteWarningSound':
sendAudioMessage('playMuteWarningSound');
break;
case 'changeVolume':
changeVolume(0.75);
break;
default:
console.log('Unknown message action:', message.action);
}
});
此外,以下是我对上下文的content-script.js
个代码.
// Helper function to play audio
function playAudio(audioFile) {
console.log(audioFile, chrome.runtime.getURL(audioFile));
const audio = new Audio(chrome.runtime.getURL(audioFile));
// Play the audio only when the EventListener for click is triggered
audio.addEventListener('click', () => {
audio.play();
});
}
// Helper function for setting volume
function setAudioVolume(volumeLevel) {
const audioElements = document.querySelectorAll('audio');
audioElements.forEach(audio => {
audio.volume = volumeLevel;
});
}
// Receive messages from your service worker
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log(message, sender, sendResponse);
switch (message.action) {
case 'playNavigationSound':
playAudio('nav.ogg');
break;
case 'playDownloadCompleteSound':
playAudio('complete.ogg');
break;
case 'playDownloadErrorSound':
playAudio('error.ogg');
break;
case 'playMuteWarningSound':
playAudio('unlock.ogg');
break;
case 'changeVolume':
setAudioVolume(message.volume);
break;
default:
console.log('Unknown message action:', message.action);
}
});
最后,这是我的manifest.json
个文件.
{
"manifest_version": 3,
"name": "PopupSound",
"description": "Plays a click sound when you click on a link. Also plays a trumpet sound when you finish downloading a file.",
"author": "Michael Gunter",
"version": "3.0",
"offline_enabled": true,
"default_locale": "en",
"icons": {
"46": "icon_46.png",
"96": "icon_96.png",
"128": "icon_128.png"
},
"background": {
"service_worker": "service-worker.js"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content-script.js"],
"run_at": "document_end"
}],
"web_accessible_resources": [{
"resources": [
"*.ogg"
],
"matches": [
"<all_urls>"
],
"extensions": []
}],
"permissions": [
"webNavigation",
"downloads",
"tabs",
"activeTab"
],
"host_permissions": ["<all_urls>"],
"content_security_policy": {}
}
我差一点就能让程序正常工作了,但这个问题让我感到困惑.如果您能帮忙,我将不胜感激.拜托了,谢谢你.
我try 使用Chrome DevTools进行调试,但无法解决问题.