Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does GM_getTab(cb) work?

There does not appear to be any information on how these functions work. I presume these are Tampermonkey exclusive functions?
It looks like they are designed to allow communication between currently running Tampermonkey scripts; An alternative to continually polling GM_Value storage for changes. Which is a really interesting idea.

But I have no idea how to use them; What values they take in, and what sort of object you get back?
How do you use these three functions, and am I right in their purpose?

GM_getTab(cb)
Get a object that is persistent as long as this tab is open.

GM_saveTab(tab)
Save the tab object to reopen it after a page unload.

GM_getTabs(cb)
Get all tab objects in an array to communicate with other scrips instances.

(http://forum.tampermonkey.net/viewtopic.php?f=16&t=74)

like image 394
Jonathon Avatar asked Sep 11 '14 17:09

Jonathon


1 Answers

I'd never attempted to use them, but looking at the code these appear to be the ability to store/get whatever you would like from each tab and also get everything stored in this fashion by all tabs.

On two chrome consoles, I have run the following:

var this_tab_data, all_tabs, n;

GM_getTab(function (o) {
    this_tab_data = o;
    n = this_tab_data.rand = Math.random();
    GM_saveTab(this_tab_data);
    console.info(this_tab_data);

    GM_getTabs(function (db) {
        all_tabs = db;
        console.info(n);
        for (var i in all_tabs) {
            if (all_tabs[i].rand === n) console.info("I bet I am the tab named: " + i);
            else console.info("Other tab: " + i + " has value: " + all_tabs[i].rand);
        }
    });
});

Result (in tab 2):

Object {rand: 0.9303610376082361}
VM779:11 0.9303610376082361
VM779:14 Other tab: 366 has value: 0.417106909211725
VM779:13 I bet I am the tab named: 371

I added access in the chrome console using this user script, (based on the instructions on the indicated @match page):

// ==UserScript==
// @name       My Fancy New Userscript
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://stackoverflow.com/questions/14059078/use-the-tampermonkey-api-from-the-chrome-console
// @copyright  2012+, You
// ==/UserScript==

unsafeWindow.GM_getTab = GM_getTab;
unsafeWindow.GM_saveTab = GM_saveTab;
unsafeWindow.GM_getTabs = GM_getTabs;

As a side note, I see that this data remains accessible with GM_getTabs() after I close the tabs that set it. I'm not sure I would count on that, but I probably would minimize what I left behind.

like image 194
lossleader Avatar answered Oct 26 '22 17:10

lossleader