如何使用Tampermonkey重新加载其他选项卡? [英] How to Use Tampermonkey to reload another tab?
问题描述
我想使用 Tampermonkey
在当前选项卡更改时刷新某个选项卡.
I want to refresh a certain tab when the current tab changes, using Tampermonkey
.
我是Duolingo的用户,但对他们对Crown系统的新更改不满意,并且我不喜欢他们带有"Practice"按钮的算法.
I am a user of Duolingo, but I am not happy with their new change of the Crown system, and I don't like their algorithm with the 'Practice' button.
因此,我使用网站 duome.eu 选择最弱的一个进行审查.
So, I use the site duome.eu to choose the weakest to review.
类似于此页上的内容:
https://duome.eu/example/progress
Like on this page:
https://duome.eu/example/progress
此网站上的信息基于用户在以下方面的进度duolingo.com.
The information on this site was based on the progress of the user on duolingo.com.
我单击duome页面上的链接以打开duolingo网站以查看技能.
完成一项技能的检查后,我希望duome.eu页面重新加载以重新计算我的进度.
I click the link on the duome page to open duolingo site to review the skill.
After finished reviewing one skill, I'd like the page of duome.eu to reload to recalculate my progress.
我该如何实现?
我也欢迎其他想法,谢谢:)
Also I'm open to other ideas, thanks in advance :)
推荐答案
您可以通过以下方式实现:
You can do this by:
- 将您的Tampermonkey脚本设置为在两个域上运行.
- 使用
GM_setValue()
在脚本实例之间进行通讯 Doc 和GM_addValueChangeListener()
文档 ,以及可选的GM_getValue()
Doc .
- Set your Tampermonkey script to run on both domains.
- Communicate between script instances using
GM_setValue()
Doc andGM_addValueChangeListener()
Doc and, optionally,GM_getValue()
Doc.
例如,假设您要监视此随机问题,并在您每次点击投票按钮或喜欢的明星时重新加载其时间轴页面.
Say, for example, that you wanted to monitor this random question, and reload its timeline page every time you clicked a vote button or the favorite star.
该可以正常运行的Tampermonkey脚本可以做到:
// ==UserScript==
// @name _Cross tab, cross domain script communication
// @match *://stackoverflow.com/questions/521295/*
// @match *://stackoverflow.com/posts/521295/timeline
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_setValue
// @grant GM_addValueChangeListener
// ==/UserScript==
const xmssionKey = "Last vote-button click event";
//-- Are we on the "interactive" page/site/domain or the "monitoring" one?
if (location.pathname.includes ("questions") ) {
//-- On the "interactive page/tab...
//-- Hook into the page's vote and favorite buttons:
$(".inner-content").on ("click", ".vote a", zEvent => {
var tmStamp = new Date ().getTime ();
var ctMessage = `Button ${zEvent.target.className} clicked - ${tmStamp}`;
console.log (ctMessage);
//-- Send message to monitoring tab.
GM_setValue (xmssionKey, ctMessage);
} );
}
else {
//-- On the "monitoring" page/tab...
//-- Listen for new message:
GM_addValueChangeListener (
xmssionKey, (keyName, oldValue, newValue, bRmtTrggrd) => {
console.log (`Received new event: ${newValue}`);
//-- User feedback, esp useful with time delay:
document.title = "Reloading...";
/*-- Important:
May need to wait 1 to 300 seconds to allow for
web-app / database / API delays and/or caching.
1222 == 1.2 seconds
*/
setTimeout ( () => {location.reload(); }, 1222);
} );
}
请注意,对于此特定示例,两个页面都在同一个域中(仅是为了使每个人都更容易运行演示脚本),但是代码/策略对于跨域页面也同样有效.
这篇关于如何使用Tampermonkey重新加载其他选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!