Diskusie » Vývoj

Different user script for different tabs

§
Pridaný: 15.03.2017
Upravený: 22.03.2017

Different user script for different tabs

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

§
Pridaný: 21.03.2017

I think this would be better for the Development section.

Can you give us more details? Is it the exact same address?

§
Pridaný: 27.03.2017
Upravený: 31.03.2017

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

Pridať odpoveď

Aby ste mohli pridať odpoveď, prihláste sa.