TABS is always a dictionary

This commit is contained in:
buzz-lightsnack-2007 2024-04-24 17:10:24 +08:00
parent ea6dd9be93
commit fc8983c646

View file

@ -1,41 +1,43 @@
export default class Tabs {
/* Add an action listener.
/* Add an action listener.
@param {string} event The event to listen for
@param {function} callback The callback function
*/
static addActionListener(event, callback) {
chrome.tabs[event].addListener((tabId, info, tab) => {callback({"tabId": tabId, "info": info, "tab": tab})})
@param {string} event The event to listen for
@param {function} callback The callback function
*/
static addActionListener(event, callback) {
chrome.tabs[event].addListener((tabId, info, tab) => {callback({"tabId": tabId, "info": info, "tab": tab})})
};
/* Create a new tab.
/* Create a new tab.
@param {string} URL The URL to open
@param {number} index The index of the tab
@param {boolean} pinned Whether the tab is pinned
*/
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;
@param {string} URL The URL to open
@param {number} index The index of the tab
@param {boolean} pinned Whether the tab is pinned
*/
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);
}
// Create the tab.
chrome.tabs.create(OPTIONS);
}
/* Filters all tabs and returns a result.
/* 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 != null) ? filters : { active: true, currentWindow: true};
let TABS = {};
@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 != null) ? 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).includes(`num`)) ? index >= 0 : false)) ? TABS.all[index] : ((Array.isArray(TABS.all) ? (TABS.all).length > 0 : false) ? TABS.all : null);
TABS.all = await chrome.tabs.query(filters);
TABS.result = (index != null)
? TABS.all[Math.abs(parseFloat(index))]
: TABS.all;
return (TABS.result);
}
return (TABS.result);
}
}