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

Support only switching to tabs in the same window #9

Open
wants to merge 2 commits 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
59 changes: 41 additions & 18 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var timer;
var slowswitchForward = false;

var initialized = false;
var options = {};

var loggingOn = false;

Expand Down Expand Up @@ -155,25 +156,35 @@ var doIntSwitch = function() {
} 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();
chrome.tabs.query({lastFocusedWindow: true, active: true}, function(activeTabs) {
var activeTab = activeTabs[0];
if (options.onlySameWindow && !activeTab) {
return;
}
});
tabIdToMakeActive = mru[intSwitchCount];
chrome.tabs.get(tabIdToMakeActive, function(tab) {
if(tab) {
if (options.onlySameWindow && (tab.windowId !== activeTab.windowId || tab.id === activeTab.id)) {
doIntSwitch();
} else {
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();
}
});
});


}
Expand Down Expand Up @@ -278,6 +289,18 @@ var initialize = function() {
});
CLUTlog("MRU after init: "+mru);
});

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;
}
});
}
}

Expand Down
9 changes: 7 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"short_name": "CLUT",

"permissions": [
"tabs"
"tabs",
"storage"
],
"content_security_policy" : "script-src 'self' https://ssl.google-analytics.com; object-src 'self'",
"icons": {
Expand Down Expand Up @@ -43,5 +44,9 @@
},
"browser_action": {
"default_icon": "icon16.png"
},
"options_ui": {
"page": "options.html",
"open_in_tab": false
}
}
}
16 changes: 16 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!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>
27 changes: 27 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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);