admin管理员组文章数量:1410712
I have written a GreaseMonkey (javascript) userscript that runs in a browser (Firefox) tab directed at www.site1/stuff
. Another tab is directed at www.site1
(without the stuff
), and is not a child window of the first (e.g., was not opened through the userscript running on the first tab). The userscript runs (independently?) on both tabs.
I would like the userscript execution on the first browser tab to pass a string variable to the second browser tab. While GM_setValue
and GM_getValue
work well for storage/retrieval within a single userscript, that storage area does not seem to be accessible to the other execution of the userscript. localStorage
suffers the same failure. For an explicit example:
When the userscript detects that it is running on
www.site1/stuff
, it places a value into storage:GM_setValue('parValue', 'aaabbbccc');
After the first tab is fully loaded and has ample time to place this value into storage, the second tab is opened manually. When the userscript detects that this second tab is running on
www.site1
(withoutstuff
), the code tries to retrieve the value:var parVal = GM_getValue('parValue')
. In my userscript, parVal would have anull
value; each userscript execution appears to use different storage areas.
How do I achieve this seemingly simple task of getting both executions of this same userscript to safe/retrieve from a mon storage area under the following constraints:
- The
stuff
at the end of the URL of the first tab can change at-will by the user (writing separate userscripts tailored for every conceivablestuff
possibility would be impossible). - The tabs will never have a parent/child relationship, as they were generated independently (technically, the second tab is a grandchild of the first tab, but I have no idea of what the window names of the 2 tabs are or how to reference them in code).
- using javascript running in GreaseMonkey userscript
Is there some kind of global, cross-tab storage area that can be used, that could be implemented in a GreaseMonkey userscript? In theory, should GM_setValue
be applicable to this situation? I've spent substantial time poring over the answers to the following related SO questions, but was unable to find a solution that works for the above set of conditions and/or can be implemented into a GreaseMonkey userscript: Communication between tabs or windows, JavaScript: sharing data between tabs, , Sending a message to all open windows/tabs using JavaScript,
I have written a GreaseMonkey (javascript) userscript that runs in a browser (Firefox) tab directed at www.site1./stuff
. Another tab is directed at www.site1.
(without the stuff
), and is not a child window of the first (e.g., was not opened through the userscript running on the first tab). The userscript runs (independently?) on both tabs.
I would like the userscript execution on the first browser tab to pass a string variable to the second browser tab. While GM_setValue
and GM_getValue
work well for storage/retrieval within a single userscript, that storage area does not seem to be accessible to the other execution of the userscript. localStorage
suffers the same failure. For an explicit example:
When the userscript detects that it is running on
www.site1./stuff
, it places a value into storage:GM_setValue('parValue', 'aaabbbccc');
After the first tab is fully loaded and has ample time to place this value into storage, the second tab is opened manually. When the userscript detects that this second tab is running on
www.site1.
(withoutstuff
), the code tries to retrieve the value:var parVal = GM_getValue('parValue')
. In my userscript, parVal would have anull
value; each userscript execution appears to use different storage areas.
How do I achieve this seemingly simple task of getting both executions of this same userscript to safe/retrieve from a mon storage area under the following constraints:
- The
stuff
at the end of the URL of the first tab can change at-will by the user (writing separate userscripts tailored for every conceivablestuff
possibility would be impossible). - The tabs will never have a parent/child relationship, as they were generated independently (technically, the second tab is a grandchild of the first tab, but I have no idea of what the window names of the 2 tabs are or how to reference them in code).
- using javascript running in GreaseMonkey userscript
Is there some kind of global, cross-tab storage area that can be used, that could be implemented in a GreaseMonkey userscript? In theory, should GM_setValue
be applicable to this situation? I've spent substantial time poring over the answers to the following related SO questions, but was unable to find a solution that works for the above set of conditions and/or can be implemented into a GreaseMonkey userscript: Communication between tabs or windows, JavaScript: sharing data between tabs, https://superuser./questions/1005448/can-a-greasemonkey-script-know-whats-been-loaded-into-another-tab, Sending a message to all open windows/tabs using JavaScript,
-
5
that storage area does not seem to be accessible to the other execution of the userscript
- you are mistaken ... GM_setValue stores values per script, regardless of URL - same script, same storage – Jaromanda X Commented Oct 17, 2017 at 5:52 -
2
localStorage
works and provides themessage
event onwindow
, making for a great "IPC" solution – dandavis Commented Oct 17, 2017 at 6:17 -
2
Thanks @JaromandaX. I just didn't see that expected behavior in my code. I will make a pared-down userscript that just tests this functionality and post here. I could very well be using
GM_setValue
andGM_getValue
incorrectly. – PMM Commented Oct 17, 2017 at 6:35 -
2
Thanks @dandavis. For my particular application, having a message event isn't necessary (I don't think), but thanks for your ment, as I was wondering how
localStorage
andGM_getValue/setValue
differ. And who knows, as I dig further into my code development, maybe I will discover that I indeed needmessage
event onwindow
. – PMM Commented Oct 17, 2017 at 6:38 -
2
To my surprise, my stripped-down userscript that focuses on 'GM_setValue` functionality works exactly as @JaromandaX describes. (I haven't done the same test with
localStorage
). The test was added to the re-edited question above (in case it helps others). – PMM Commented Oct 17, 2017 at 7:15
1 Answer
Reset to default 7Turns out that 'GM_setValue/getValue' indeed allow information to be shared between 2 tabs in which the same userscript is running in parallel. I proved it with the following test code. I started with a tab directed to www.google.
, got the alerts, and then opened up another tab in the same browser window and directed the URL to www.yahoo.. The alert indicated that the value was successfully retrieved from storage from where the userscript executing on the google.
had placed it.
// ==UserScript==
// @name hello world
// @namespace http://www.sharelatex.
// @include https://www.google./*
// @include https://www.yahoo./*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
if (window.location.href.indexOf("google.") != -1) { // on Google URL
alert("on Google site, storing value");
// you will see the above alert verifying that code understands
// the code is running on the google. tab
GM_setValue('passValue', 'aabbcc');
} else {
alert("on Yahoo site, retrieving value");
// the above alert will be seen, verifying that the code
// understands the code is running on the yahoo. tab
var pvalue = GM_getValue('passValue');
alert("The retrieved value is " + pvalue);
// the above alert should show aabbcc, verifying that the
// userscript running on google. successfully stored the value
// and the script on yahoo. successfully retrieved it.
}
本文标签: javascriptHow to pass information between 2 tabs running same GreaseMonkey userscriptStack Overflow
版权声明:本文标题:javascript - How to pass information between 2 tabs running same GreaseMonkey userscript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744935552a2633160.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论