Skip to content

Commit

Permalink
增加按钮:静音/解除当前页面静音
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Nov 4, 2022
1 parent c418e48 commit 746b7ea
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

images/logo0*
images/logo1*
4 changes: 3 additions & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
已经上传Edge商店 https://microsoftedge.microsoft.com/addons/detail/ahnjeefliifcofmjceffghigncgoigmj


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


![img](./screen.png)

Expand Down
6 changes: 6 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
},
"audible": {
"message": "Skip no audio tabs"
},
"muteactive": {
"message": "Mute this tab"
},
"unmuteactive": {
"message": "UnMute this tab"
}
}
6 changes: 6 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
},
"audible": {
"message": "跳过不发音标签"
},
"muteactive": {
"message": "静音当前标签页"
},
"unmuteactive": {
"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.2.1",
"version": "0.2.2",
"update_url": "https://edge.microsoft.com/extensionwebstorebase/v1/crx",
"manifest_version": 2,
"minimum_chrome_version": "22.0",
Expand Down
3 changes: 2 additions & 1 deletion popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
<div class="setting" id="auto"></div>
<div class="setting" id="audible"></div>
<div class="setting" id="mute"></div>
<div class="setting" id="unmute"></div>
<div class="setting" id="unmute"></div>
<div class="setting" id="muteactive"></div>
<!--
<label class="setting"><input id="auto" type="checkbox">自动静音后台页</label>
<label class="setting"><input id="mute" type="checkbox">静音全部标签页</label>
Expand Down
23 changes: 11 additions & 12 deletions scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@ sw2(sw_audible);

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

try {
let tabId = activeInfo.tabId;
chrome.tabs.get(tabId, async (tab) => {

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

// 不智能跳过,或者智能跳过但是这个页面发音
if(!sw_audible || tab.audible){
if(tabNow != tabId){
await chrome.tabs.update(tabNow, { muted:true} );
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}`);
});
console.log(`Tab ${tab.id} is ${muted ? 'muted' : 'unmuted'} audiable=${tab.audible}`);
});

} catch (e) {
console.log("Error-Mute-Tab-" + tabNow + "\n" + e);
Expand All @@ -43,9 +43,9 @@ chrome.tabs.onActivated.addListener(function (activeInfo) {

// 如果智能跳过,需要检查前台标签内容更新时自动发声
chrome.tabs.onUpdated.addListener(function (id, info, tab) {
if(sw_audible && id == tabActive && id!=tabNow && tab.audible){
chrome.tabs.update(id, { muted:false });
chrome.tabs.update(tabNow, { muted:true} );
if (sw_audible && id == tabActive && id != tabNow && tab.audible) {
chrome.tabs.update(id, { muted: false });
chrome.tabs.update(tabNow, { muted: true });
tabNow = tabActive;
}
});
Expand Down Expand Up @@ -78,7 +78,6 @@ function sw2(sw) {
}
}


function muteAll(mute) {
chrome.windows.getAll({ populate: true }, function (windows) {
windows.forEach(function (window) {
Expand Down
104 changes: 74 additions & 30 deletions scripts/popup.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,83 @@
var sw_on =true;
if(localStorage['sw_on']=='false')
sw_on=false ;
var sw_on = true;
if (localStorage['sw_on'] == 'false')
sw_on = false;

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

let queryActiveOptions = { active: true, lastFocusedWindow: true };

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

// 自动静音
var auto = document.getElementById('auto');
auto.textContent = chrome.i18n.getMessage("autoMute");

// 静音全部
var mute = document.getElementById('mute');
mute.textContent = chrome.i18n.getMessage("allMute");

// 恢复全部
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){
// 静音/恢复当前标签
var muteactive = document.getElementById('muteactive');
muteactive.textContent = chrome.i18n.getMessage("unmuteactive");

chrome.tabs.query(queryActiveOptions, ([tab]) => {
if (chrome.runtime.lastError)
console.error(chrome.runtime.lastError);

console.log(tab.mutedInfo);
let activeMute = tab.mutedInfo.muted;
if (activeMute)
muteactive.setAttribute("sw", "off");
else
muteactive.setAttribute("sw", "on");
});


let bg = chrome.extension.getBackgroundPage();

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

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

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

var bg = chrome.extension.getBackgroundPage();
bg.sw(sw_on);

if (sw_on){
sw_on = autoMute(!sw_on);
if (sw_on) {
auto.setAttribute("sw", "on");
} else {
} else {
auto.setAttribute("sw", "off");
}
console.log('auto');
}


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

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

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

mute.onclick = function () {
Expand All @@ -69,8 +89,24 @@ document.addEventListener('DOMContentLoaded', function () {
console.log('unute');
muteAll(false);

sw_on=false;
auto.setAttribute("sw", "on");
sw_on = autoMute(false)
auto.setAttribute("sw", "off");
}

muteactive.onclick = function () {

chrome.tabs.query(queryActiveOptions, ([tab]) => {
if (chrome.runtime.lastError)
console.error(chrome.runtime.lastError);

console.log(tab.mutedInfo);
let activeMute = !tab.mutedInfo.muted;
chrome.tabs.update(tab.id, { muted: activeMute });
if (activeMute)
muteactive.setAttribute("sw", "off");
else
muteactive.setAttribute("sw", "on");
});
}


Expand All @@ -79,7 +115,7 @@ document.addEventListener('DOMContentLoaded', function () {

function muteAll(mute) {

if(!mute){
if (!mute) {
var bg = chrome.extension.getBackgroundPage();
bg.sw(false);
}
Expand All @@ -91,4 +127,12 @@ function muteAll(mute) {
});
});
});
}
}

function autoMute(on) {
sw_on = on;
var bg = chrome.extension.getBackgroundPage();
bg.sw(sw_on);
console.log('autoMute: ' + sw_on);
return sw_on;
}

0 comments on commit 746b7ea

Please sign in to comment.