add a module on tab management

This commit is contained in:
buzz-lightsnack-2007 2024-04-22 21:58:17 +08:00
parent ec730cfc5d
commit 9cf8abee70
2 changed files with 34 additions and 1 deletions

View file

@ -1,5 +1,8 @@
// Manage the sidepanel. // Manage the sidepanel.
// The sidepanel is what I'll call the Sidebar. // The sidepanel is what I'll call the Sidebar.
import Tabs from './tabs.js';
export default class Sidebar { export default class Sidebar {
/* /*
Create a new sidebar. Create a new sidebar.
@ -11,7 +14,7 @@ export default class Sidebar {
chrome.sidePanel.setOptions({ path: PATH }); chrome.sidePanel.setOptions({ path: PATH });
// Grab the current tab ID. // Grab the current tab ID.
chrome.tabs.query({ active: true, currentWindow: true }, function ([TAB]) { Tabs.query({ active: true, currentWindow: true }, 0).then((TAB) => {
chrome.sidePanel.open({windowId: TAB.id}); chrome.sidePanel.open({windowId: TAB.id});
this.root = PATH; this.root = PATH;
}); });

30
scripts/GUI/tabs.js Normal file
View file

@ -0,0 +1,30 @@
export default class Tabs {
static addActionListener(event, callback) {
(event.toLowerCase().contains(`update`)) ? chrome.tabs.onUpdated.addListener(async (tabId, info, tab) => {callback({"tabId": tabId, "info": info, "tab": tab})}) : false;
};
static create(URL, index, pinned = false) {
((typeof index).includes(`obj`) && index != null && !Array.isArray(index))
// Set the options.
let OPTIONS = {url: URL, active: true, pinned: pinned};
(index) ? OPTIONS.index = index : null;
// Create the tab.
chrome.tabs.create(OPTIONS);
}
/* Filters all tabs and returns a result.
@param {Object} filters The filters on tab search
@param {number} index the tab number to return
*/
static async query(filters, index) {
filters = ((typeof filters).includes(`obj`)) ? filters : { active: true, currentWindow: true};
let TABS = {};
TABS.all = await chrome.tabs.query(filters);
TABS.result = ((Array.isArray(TABS.all) ? (TABS.all).length > 0 : false) && ((index != null && (typeof index).contains(`num`)) ? index >= 0 : false)) ? TABS.all[index] : ((Array.isArray(TABS.all) ? (TABS.all).length > 0 : false) ? TABS.all : null);
return (TABS.result);
}
}