Skip to content

Commit

Permalink
feature update
Browse files Browse the repository at this point in the history
  • Loading branch information
edge_sky authored and edge_sky committed Feb 5, 2024
1 parent affa9ef commit 094e5d4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
40 changes: 23 additions & 17 deletions AutoClose/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ chrome.windows.onRemoved.addListener(function (windowId) {
});

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
if (tab.status === 'complete') {
if (tab.status === 'complete' && tab.active && isReady) {
isReady = false;
// get state of is new tab; it sees like run more than one time, it may a problem
chrome.storage.local.get(['newOpen', 'isBlackList', 'blackList', 'whiteList'], function (data) {
if (isReady && data.newOpen && tab.active) {
isReady = false;
if (data.newOpen) {
// set state of new tab
chrome.storage.local.set({newOpen: false});

Expand All @@ -30,19 +30,23 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
// black list mode
for (let i = 0; i < tabs.length; i++) {
for (let j = 0; j < data.blackList.length; j++) {
if (tabs[i].id !== tab.id && !matchDomain(data.blackList[j], tabs[i].url)) {
if (tabs[i].id !== tab.id && matchDomain(data.blackList[j], tabs[i].url)) {
chrome.tabs.remove(tabs[i].id);
}
}
}
} else {
let isMatch = false;
// white list mode
for (let i = 0; i < tabs.length; i++) {
for (let j = 0; j < data.whiteList.length; j++) {
if (tabs[i].id !== tab.id && matchDomain(data.whiteList[j], tabs[i].url)) {
chrome.tabs.remove(tabs[i].id);
if (!matchDomain(data.whiteList[j], tabs[i].url)) {
isMatch = true;
}
}
if (!isMatch && tabs[i].id !== tab.id) {
chrome.tabs.remove(tabs[i].id);
}
}
}

Expand All @@ -54,22 +58,24 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
chrome.storage.local.set({closeNum: data.closeNum + tabs.length - 1});
});
});
isReady = true;
}
});
isReady = true;
}
});

chrome.runtime.onInstalled.addListener(function () {
chrome.storage.local.set({
newOpen: true,
closeTimes: 0,
closeNum: 0,
lastCloseNum: 0,
isBlackList: true,
blackList: [],
whiteList: [],
});
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason === 'install') {
chrome.storage.local.set({
newOpen: true,
closeTimes: 0,
closeNum: 0,
lastCloseNum: 0,
isBlackList: true,
blackList: [],
whiteList: [],
});
}
});

// Url domain match
Expand Down
2 changes: 1 addition & 1 deletion AutoClose/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "AutoCloseTap",
"version": "0.9",
"version": "1.0",
"manifest_version": 3,
"description": "Tab-groups will be fixed in default,this extension can auto close pages when you close browser",
"permissions": [
Expand Down
2 changes: 1 addition & 1 deletion AutoClose/popup.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
width: 125px;
width: 175px;
height: 200px;
margin: 20px 30px 20px 20px;
padding: 0;
Expand Down
1 change: 1 addition & 0 deletions AutoClose/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<span class="slider round"></span>
</label>
</div>
<div id="tip" style="font-size: 10px">tips:</div>
<div id="list"></div>
</div>
</div>
Expand Down
25 changes: 24 additions & 1 deletion AutoClose/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ chrome.storage.local.get(['closeTimes', 'closeNum', 'lastCloseNum', 'isBlackList
if (data.isBlackList) {
document.getElementById('listSwitch').checked = false;
document.getElementById('displayList').innerText = '黑名单';
document.getElementById('tip').innerText = 'Tips: 以下网址会被关闭';
} else {
document.getElementById('listSwitch').checked = true;
document.getElementById('displayList').innerText = '白名单';
document.getElementById('tip').innerText = 'Tips: 以下网址不会被关闭';
}
});

document.getElementById('listSwitch').addEventListener('change', function (event) {
if (event.target.checked) {
chrome.storage.local.set({isBlackList: false});
document.getElementById('displayList').innerText = '白名单';
document.getElementById('tip').innerText = 'Tips: 以下网址不会被关闭';
} else {
chrome.storage.local.set({isBlackList: true});
document.getElementById('displayList').innerText = '黑名单';
document.getElementById('tip').innerText = 'Tips: 以下网址会被关闭';
}
displayList();
});
Expand All @@ -44,7 +48,26 @@ function displayList() {
listElement.innerHTML = '';
for (let i = 0; i < list.length; i++) {
let listItem = document.createElement('div');
let deleteButton = document.createElement('button');
deleteButton.textContent = '-';
deleteButton.addEventListener('click', function () {
// 从列表中删除对应的项
list.splice(i, 1);
// 更新存储的列表
if (data.isBlackList) {
chrome.storage.local.set({blackList: list});
} else {
chrome.storage.local.set({whiteList: list});
}
// 重新显示列表
displayList();
});

listItem.style.width = '140px';
deleteButton.style.width = '25px';

listItem.textContent = list[i];
listItem.appendChild(deleteButton);
listElement.appendChild(listItem);
}

Expand All @@ -53,7 +76,7 @@ function displayList() {
let addButton = document.createElement('button');
addButton.textContent = '+';

inputElement.style.width = '90px';
inputElement.style.width = '140px';
addButton.style.width = '25px';
inputElement.id = 'input';
addButton.onclick = function () {
Expand Down

0 comments on commit 094e5d4

Please sign in to comment.