Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Different user script for different tabs

edited March 22 in Development Firefox
Sir i need in Firefox to use Different user script for different tabs of the same address is there any way

Comments

  • I think this would be better for the Development section.

    Can you give us more details? Is it the exact same address?
  • I don't think you can do it based on tab, but if you had different addresses in some way (by adding your own parameter that the page ignores), then sure.
  • edited March 31 Chrome
    Itis not possible to use different userscript for different tabs on the same address. HOWEVER, it is possible to make 1 userscript behave differently for each tab. You just need some variable which persist over all tabs (GM_setValue/GM_getValue are good for this purpose), and some functions to call depending on its value.
    Simple example:

    var idx = GM_getValue('3_tab_functions',0);
    GM_setValue('3_tab_functions',idx + 1) ;
    idx=idx%3;
    if (idx == 0) { func0(); }
    else if (idx == 1) { func1(); }
    else if (idx == 2) { func2(); }

    If you really want/need to keep things in different scripts, then, using the same concept, you can have all the scripts to run, but have all but the one(s) you want auto-die.
    eg.
    // Script 1 for tab 1
    var idx = GM_getValue('3_tab_functions',0);
    GM_setValue('3_tab_functions',idx + 1);
    idx=idx%3;
    // Die if we aren't suppose to run
    if (idx !== 0) return 0;
    // Your Code Here

    // Script 2 for tab 2
    var idx = GM_getValue('3_tab_functions',0);
    GM_setValue('3_tab_functions',idx + 1);
    idx=idx%3;
    // Die if we aren't suppose to run
    if (idx !== 1) return 0;
    // Your Code Here

    // Script 3 for tab 3
    var idx = GM_getValue('3_tab_functions',0);
    GM_setValue('3_tab_functions',idx + 1);
    idx=idx%3;
    // Die if we aren't suppose to run
    if (idx !== 3) return 0;
    // Your Code Here


    This is a simple example and can be vastly expanded. For instance, you can create a tab-to-tab communication protocol with it, which can be used to determine the order in which the scripts/tabs were loaded, how many tabs are actively using the script, etc.

    --EDIT--
    One more, this time a more complete and tested script. This one additionally does not cycle, meaning each function (or script) should run only in 1 tab. Once all functions are accounted for, additional tabs will simply not do anything. There is a small race condition in here, you could use storage events (refer to https://html.spec.whatwg.org/multipage/webstorage.html) to overcome this.

    // ==UserScript==
    // @name 3_tab_functions
    // @namespace https://myanimelist.net
    // @version 0.1
    // @description Semi-Complete example for 1 script, multiple functions. We use localStorage which gives you the ability to add storage events if needed. Refer to https://html.spec.whatwg.org/multipage/webstorage.html
    // @author You
    // @match https://myanimelist.net
    // @grant GM_getValue
    // @grant GM_setValue
    // @grant unsafeWindow
    // ==/UserScript==
    function dbg(msg) {
    console.log(msg);
    //alert(msg);
    }

    function save(namespace,data) {
    //dbg(data);
    localStorage.setItem(namespace,JSON.stringify(data));
    //GM_setValue(namespace,JSON.stringify(data));
    }
    function load(namespace) {
    var data=localStorage.getItem(namespace);
    //var data=GM_getValue(namespace);
    if (! data)
    data = {
    0:{'expires': 0, 'owner': null},
    1:{'expires': 0, 'owner': null},
    2:{'expires': 0, 'owner': null},
    };
    else {
    data = JSON.parse(data);
    dbg(data);
    }
    return data;
    }
    function register(ridx) {
    var data = load('3_tab_functions');
    var ctime = new Date()/1;
    dbg("rdx " + ridx);
    if (! isNaN(ridx)) {
    // Already registered, We just need to update the expire time
    data[ridx].expires = ctime + 1000;
    save('3_tab_functions',data);
    }
    else {
    // Not yet registered, lets try to get in.
    for (var i=0;i<Object.keys(data).length;i++) {
    if (data[i].expires < ctime) {
    data[i].expires = ctime + 1000;
    //data[i].owner = window;
    save('3_tab_functions',data);
    dbg("Got 3_tab_var idx " + i);
    ridx=i;
    break;
    }
    }
    }
    return ridx;
    }
    // Get an index, this will determin which script to run
    var idx = register();
    // Update the expire tag so another instance doesn't try to register it.
    if (! isNaN(idx)) setInterval(function(){ register(idx); }, 250);

    // Define the functions we want to run for each index
    function func0() { dbg("Got script 0 idx " + idx); }
    function func1() { dbg("Got script 1 idx " + idx); }
    function func2() { dbg("Got script 2 idx " + idx); }

    // Given the index, chose the functions we want to run.
    if (idx === 0) { func0(); }
    else if (idx === 1) { func1(); }
    else if (idx === 2) { func2(); }


Sign In or Register to comment.