Skip to content

Commit

Permalink
implement harshayburadkar#9
Browse files Browse the repository at this point in the history
  • Loading branch information
SornrasakC committed Oct 21, 2024
1 parent 1577b7d commit 17c197e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 41 deletions.
105 changes: 66 additions & 39 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var timer;
var slowswitchForward = false;

var initialized = false;
var options = {};
var currentWindowId;

var loggingOn = true;

Expand Down Expand Up @@ -95,44 +97,58 @@ chrome.runtime.onInstalled.addListener(function () {
initialize();
});

var doIntSwitch = function () {
var doIntSwitch = function (recurseLevel = 0) {
CLUTlog("CLUT:: in int switch, intSwitchCount: " + intSwitchCount + ", mru.length: " + mru.length);
if (intSwitchCount < mru.length && intSwitchCount >= 0) {
var tabIdToMakeActive;
//check if tab is still present
//sometimes tabs have gone missing
var invalidTab = true;
var thisWindowId;
if (slowswitchForward) {
decrementSwitchCounter();
} else {
incrementSwitchCounter();
}
tabIdToMakeActive = mru[intSwitchCount];
chrome.tabs.get(tabIdToMakeActive, function (tab) {
if (tab) {
thisWindowId = tab.windowId;
invalidTab = false;

chrome.windows.update(thisWindowId, { focused: true });
chrome.tabs.update(tabIdToMakeActive, { active: true, highlighted: true });
lastIntSwitchIndex = intSwitchCount;
//break;
} else {
CLUTlog(
"CLUT:: in int switch, >>invalid tab found.intSwitchCount: " +
intSwitchCount +
", mru.length: " +
mru.length
);
removeItemAtIndexFromMRU(intSwitchCount);
if (intSwitchCount >= mru.length) {
intSwitchCount = 0;
}
doIntSwitch();
}
if (!(0 <= intSwitchCount && intSwitchCount < mru.length)) return;

if (recurseLevel == 0) {
return chrome.windows.getCurrent(function (currentWindow) {
currentWindowId = currentWindow.id;
doIntSwitch(1);
});
}
if (recurseLevel > mru.length) return; // just in case

var tabIdToMakeActive;
//check if tab is still present
//sometimes tabs have gone missing
var invalidTab = true;
var thisWindowId;
if (slowswitchForward) {
decrementSwitchCounter();
} else {
incrementSwitchCounter();
}
tabIdToMakeActive = mru[intSwitchCount];

chrome.tabs.get(tabIdToMakeActive, function (tab) {
if (tab) {
thisWindowId = tab.windowId;

if (options.onlySameWindow && thisWindowId !== currentWindowId) {
return doIntSwitch(recurseLevel + 1); // skip this tab
}

invalidTab = false;

chrome.windows.update(thisWindowId, { focused: true });
chrome.tabs.update(tabIdToMakeActive, { active: true, highlighted: true });
lastIntSwitchIndex = intSwitchCount;
//break;
} else {
CLUTlog(
"CLUT:: in int switch, >>invalid tab found.intSwitchCount: " +
intSwitchCount +
", mru.length: " +
mru.length
);
removeItemAtIndexFromMRU(intSwitchCount);
if (intSwitchCount >= mru.length) {
intSwitchCount = 0;
}
doIntSwitch(recurseLevel + 1);
}
});
};

var endSwitch = function () {
Expand Down Expand Up @@ -169,6 +185,21 @@ chrome.tabs.onRemoved.addListener(function (tabId, removedInfo) {
removeTabFromMRU(tabId);
});

chrome.storage.sync.get(
{
onlySameWindow: false
},
function (items) {
options.onlySameWindow = items.onlySameWindow;
}
);

chrome.storage.onChanged.addListener(function (changes) {
for (let [key, { newValue }] of Object.entries(changes)) {
options[key] = newValue;
}
});

var addTabToMRUAtBack = function (tabId) {
var index = mru.indexOf(tabId);
if (index == -1) {
Expand Down Expand Up @@ -238,10 +269,6 @@ var printMRUSimple = async function () {
CLUTlog(tabs.map((t) => `${t.index} - ${t.title}`));
};

// async function getTabs() {
// const tabs = await Promise.all((mru || []).map(tabId => new Promise(resolve => chrome.tabs.get(tabId, resolve))));
// return tabs.filter(Boolean);
// }
async function getTabs() {
const tabs = await Promise.all(
(mru || []).map(
Expand Down
9 changes: 7 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
"manifest_version": 3,
"name": "CLUT: Cycle Last Used Tabs - MV3",
"description": "Cycle through last used Chrome tabs using keyboard shortcut.",
"version": "1.8",
"version": "2.0",
"short_name": "CLUT",
"permissions": [
"tabs"
"tabs",
"storage"
],
"icons": {
"16": "icon16.png",
Expand Down Expand Up @@ -40,5 +41,9 @@
},
"action": {
"default_icon": "icon16.png"
},
"options_ui": {
"page": "options.html",
"open_in_tab": false
}
}
17 changes: 17 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>CLUT Options</title>
</head>
<body>
<label>
<input type="checkbox" id="only-same-window" />
Only switch to tabs in the same window
</label>

<div id="status"></div>
<button id="save">Save</button>

<script src="options.js"></script>
</body>
</html>
35 changes: 35 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// https://github.com/harshayburadkar/clut-chrome-extension/pull/9/files

// Saves options to chrome.storage
function save_options() {
var onlySameWindow = document.getElementById("only-same-window").checked;
chrome.storage.sync.set(
{
onlySameWindow
},
function () {
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.textContent = "Options saved.";
setTimeout(function () {
status.textContent = "";
}, 750);
}
);
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
// Use default value color = 'red' and likesColor = true.
chrome.storage.sync.get(
{
onlySameWindow: false
},
function (items) {
document.getElementById("only-same-window").checked = items.onlySameWindow;
}
);
}
document.addEventListener("DOMContentLoaded", restore_options);
document.getElementById("save").addEventListener("click", save_options);

0 comments on commit 17c197e

Please sign in to comment.