Tampermonkey® by Jan Biniok

GM_getTab(callback)

The GM_getTab function takes a single parameter, a callback function that will be called with an object that is persistent as long as this tab is open.

GM_getTab((tab) => console.log(tab));
const t = await GM.getTab();
console.log(t);

GM_saveTab(tab, cb)

The GM_saveTab function allows a userscript to save information about a tab for later use.

The function takes a "tab" parameter, which is an object containing the information to be saved about the tab and an optional callback function "cb".

The GM_saveTab function saves the provided tab information, so that it can be retrieved later using the GM_getTab function.

Here is an example of how the GM_saveTab function might be used in a userscript:

GM_getTab(function(tab) {
    tab.newInfo = "new!";
    GM_saveTab(tab);
});
const tab = await GM.getTab();
await GM.saveTab(tab);

In this example, the GM_saveTab function is called with the tab object returned by the GM_getTab function, and a new key called "newInfo".

GM_getTabs(callback)

The GM_getTabs function takes a single parameter: a callback function that will be called with the information about the tabs.

The "tabs" object that is passed to the callback function contains objects, with each object representing the saved tab information stored by GM_saveTab.

GM_getTabs((tabs) => {
    for (const [tabId, tab] of Object.entries(tabs)) {
        console.log(`tab ${tabId}`, tab);
    }
});
const tabs = await GM.getTabs();