diff --git a/scripts/GUI/sidepanel.js b/scripts/GUI/sidepanel.js index 84a1801..2d60c75 100644 --- a/scripts/GUI/sidepanel.js +++ b/scripts/GUI/sidepanel.js @@ -1,5 +1,8 @@ // Manage the sidepanel. // The sidepanel is what I'll call the Sidebar. + +import Tabs from './tabs.js'; + export default class Sidebar { /* Create a new sidebar. @@ -11,7 +14,7 @@ export default class Sidebar { chrome.sidePanel.setOptions({ path: PATH }); // 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}); this.root = PATH; }); diff --git a/scripts/GUI/tabs.js b/scripts/GUI/tabs.js new file mode 100644 index 0000000..d0a9f90 --- /dev/null +++ b/scripts/GUI/tabs.js @@ -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); + } +} \ No newline at end of file