Skip to content

Commit

Permalink
增加开关“跳过不发音标签”,更新Chrome API
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Sep 20, 2021
1 parent e7a6bdf commit 09c52f1
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
12 changes: 8 additions & 4 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@

已经上传Edge商店 https://microsoftedge.microsoft.com/addons/detail/ahnjeefliifcofmjceffghigncgoigmj

![img](./screen.gif)

UI非常简单,一共3个按钮:
UI非常简单,一共4个按钮:
1. 是否自动静音后台页面
2. 一键静音当前打开的全部页面(只生效一次)
3. 一键恢复当前打开的全部页面(只生效一次)
2. 是否跳过不发音的标签页
3. 一键静音当前打开的全部页面(只生效一次)
4. 一键恢复当前打开的全部页面(只生效一次)

![img](./screen.png)


![img](./screen.gif)

图标来自[iconfont](https://www.iconfont.cn/), 按钮样式来自[CSS Button Generator](https://www.bestcssbuttongenerator.com/#/1)
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
},
"aboutText": {
"message": "This extension can mute background tabs automatically and unmute them automatically when the page is switched to the foreground."
},
"audible": {
"message": "Skip no audio tabs"
}
}
3 changes: 3 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
},
"aboutText": {
"message": "此扩展能够把后台标签页自动静音,当页面切换到前台时自动取消静音。"
},
"audible": {
"message": "跳过不发音标签"
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "__MSG_name__",
"short_name": "Auto Mute",
"author": "Tumuyan",
"version": "0.1",
"version": "0.2",
"update_url": "https://edge.microsoft.com/extensionwebstorebase/v1/crx",
"manifest_version": 2,
"minimum_chrome_version": "22.0",
Expand Down
1 change: 1 addition & 0 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<body>
<div id="popup-header"> Auto Mute </div>
<div class="setting" id="auto"></div>
<div class="setting" id="audible"></div>
<div class="setting" id="mute"></div>
<div class="setting" id="unmute"></div>
<!--
Expand Down
Binary file modified screen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 45 additions & 13 deletions scripts/background.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,67 @@
var tabNow = -1;
var sw_on = true;

if(localStorage['sw_on']=="false")
if (localStorage['sw_on'] == 'false')
sw_on = false;
sw(sw_on);

var sw_audible = true;
if (localStorage['audible'] == 'false')
sw_audible = false;
sw2(sw_audible);

chrome.tabs.onSelectionChanged.addListener(function (tabId, selectInfo) {
if(!sw_on)
chrome.tabs.onActivated.addListener(function (activeInfo) {
console.log("onSelectionChanged: tabid-> " + activeInfo.tabId + " sw_on="+sw_on);
if (!sw_on)
return;
console.log("onSelectionChanged: " + tabNow + "->" + tabId);

try {
chrome.tabs.update(tabNow, { muted: true });
let tabId = activeInfo.tabId;
chrome.tabs.get(tabId, async (tab) => {

let muted = tab.active?false:true;
await chrome.tabs.update(tabId, { muted });

if(!sw_audible || tab.audible){
if(tabNow != tabId){
await chrome.tabs.update(tabNow, { muted:true} );
tabNow = tabId;
}
}
console.log(`Tab ${tab.id} is ${muted ? 'muted' : 'unmuted' } audiable=${tab.audible}`);
});

} catch (e) {
console.log("Error-Mute-Tab-" + tabNow + "\n" + e);
}

chrome.tabs.update(tabId, { muted: false });
tabNow = tabId;
});

function sw(sw){
function sw(sw) {
sw_on = sw;
localStorage['sw_on']=sw_on;
localStorage['sw_on'] = sw_on;

if (sw_on) {
muteAll(true);

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tab) {
tabNow = tab.id;
chrome.tabs.update(tab.id, { muted: false });
});
}
}

if(sw_on){
function sw2(sw) {
sw_audible = sw;
localStorage['audible'] = sw_audible;

if (sw_audible) {
muteAll(true);
chrome.tabs.getSelected(function (tab) {
tabNow=tab.id;

chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tab) {
tabNow = tab.id;
chrome.tabs.update(tab.id, { muted: false });
})
});
}
}

Expand Down
31 changes: 30 additions & 1 deletion scripts/popup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
var sw_on =true;
if(localStorage['sw_on']=="false")
if(localStorage['sw_on']=='false')
sw_on=false ;

var sw_audible = true;
if(localStorage['audible']=='false')
sw_audible=false;

document.addEventListener('DOMContentLoaded', function () {

var auto = document.getElementById('auto');
Expand All @@ -12,12 +17,21 @@ document.addEventListener('DOMContentLoaded', function () {
var unmute = document.getElementById('unmute');
unmute.textContent = chrome.i18n.getMessage("unMute");

var audible = document.getElementById('audible');
audible.textContent = chrome.i18n.getMessage("audible");

if (sw_on){
auto.setAttribute("sw", "on");
} else {
auto.setAttribute("sw", "off");
}

if (sw_audible){
audible.setAttribute('sw',"on");
}else{
audible.setAttribute("sw","off");
}

auto.onclick = function () {
sw_on = !sw_on;

Expand All @@ -31,6 +45,21 @@ document.addEventListener('DOMContentLoaded', function () {
}
console.log('auto');
}


audible.onclick = function(){
sw_audible = !sw_audible;

var bg=chrome.extension.getBackgroundPage();
bg.sw2(sw_audible);

if (sw_audible){
audible.setAttribute('sw',"on");
}else{
audible.setAttribute("sw","off");
}
}

mute.onclick = function () {
console.log('mute');
muteAll(true);
Expand Down

0 comments on commit 09c52f1

Please sign in to comment.