Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrated to the latest Manifest-V3 of chrome extension (fix #1) #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions background.html

This file was deleted.

66 changes: 47 additions & 19 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,51 @@
var hasSetRain = false,isPlay = false,player = document.getElementById('player');
let offscreenPromise;
let hasOffscreen = false;
let isPlaying = false;

function togglePlay(){
if(isPlay){
isPlay = false;
player.pause();
chrome.browserAction.setIcon({path: 'assets/sun.png'});
chrome.browserAction.setTitle({title:'Raining makes everything better!'});
}else{
isPlay = true;
player.play();
chrome.browserAction.setIcon({path: 'assets/rain.png'});
chrome.browserAction.setTitle({title:"Listen it's raining!"});
chrome.action.onClicked.addListener(async () => {
if (!isPlaying) {
if (!hasOffscreen) {
// 创建离屏文档
await ensureOffscreen();
hasOffscreen = true;
}
}
// 发送消息给离屏文档开始播放
await chrome.runtime.sendMessage({ action: 'play' });
isPlaying = true;
chrome.action.setIcon({ path: 'assets/rain.png' });
chrome.action.setTitle({ title: "Listen it's raining!" });
} else {
// 发送消息给离屏文档停止播放
await chrome.runtime.sendMessage({ action: 'pause' });
isPlaying = false;
chrome.action.setIcon({ path: 'assets/sun.png' });
chrome.action.setTitle({ title: 'Raining makes everything better!' });
}
});

chrome.browserAction.onClicked.addListener(function(tab) {
if(!hasSetRain){
player.setAttribute('src','assets/Raining.mp3');
hasSetRain = true;
}
togglePlay();
// 监听离屏文档关闭的消息
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'closed') {
hasOffscreen = false;
isPlaying = false;
chrome.action.setIcon({ path: 'assets/sun.png' });
chrome.action.setTitle({ title: 'Raining makes everything better!' });
}
});

// fix: Error: Only a single offscreen document may be created.
async function ensureOffscreen() {
if (offscreenPromise) {
return await offscreenPromise;
}
offscreenPromise = chrome.offscreen.createDocument({
url: 'offscreen.html',
reasons: ['AUDIO_PLAYBACK'],
justification: 'Play rain sound'
});
try {
await offscreenPromise;
} finally {
offscreenPromise = null;
}
}
16 changes: 9 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
{
"name": "It's Raining"
,"version":"1.6"
,"manifest_version":2
,"version":"3.0"
,"manifest_version":3
,"description":"Rain Makes Everything Better"

,"icons":{
"16":"assets/16.png"
,"48":"assets/48.png"
,"128":"assets/128.png"
}
,"browser_action": {
,"action": {
"default_icon": "assets/sun.png"
,"default_title":"Raining Makes Everything Better"
,"default_title":"Raining makes everything better!"
}
,"permissions":[
"background"
"offscreen"
]

,"options_page": "options.html"
,"options_ui": {
"page": "options.html"
}
,"background": {
"page":"background.html"
"service_worker": "background.js"
}
}
9 changes: 9 additions & 0 deletions offscreen.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Offscreen Document</title>
</head>
<body>
<script src="offscreen.js"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions offscreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let audio;

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'play') {
if (!audio) {
audio = new Audio(chrome.runtime.getURL('assets/Raining.mp3'));
audio.loop = true;
}
audio.play();
} else if (message.action === 'pause') {
if (audio) {
audio.pause();
}
}
});

// 监听离屏文档关闭事件
window.addEventListener('beforeunload', () => {
chrome.runtime.sendMessage({ action: "closed" }); // 通知 Service Worker 离屏文档已关闭
});