如何使用Tampermonkey重新加载其他选项卡?

Tiw*_*Tiw 3 javascript tampermonkey

我想在当前选项卡更改时刷新某个选项卡Tampermonkey

我是Duolingo的用户,但对他们对Crown系统的新更改不满意,并且我不喜欢使用“ Practice”按钮的算法。

因此,我使用网站duome.eu来选择最弱的评论。

就像在此页面上一样:https :
    //duome.eu/example/progress

该网站上的信息基于duolingo.com上用户的进度。

我单击duome页面上的链接以打开duolingo网站以查看该技能。
完成一项技能的复习后,我希望duome.eu页面重新加载以重新计算我的进度。

我该如何实现?

我也欢迎其他想法,在此先感谢:)

Bro*_*ams 6

您可以通过以下方式做到这一点:

  1. 将您的Tampermonkey脚本设置为在两个域上均运行
  2. 使用GM_setValue()DocGM_addValueChangeListener()Doc以及可选的GM_getValue()Doc在脚本实例之间进行通信

例如,假设您要监视随机问题,并每次单击投票按钮或喜欢的明星时重新加载其时间轴页面

这个完整的有效的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);
    } );
}
Run Code Online (Sandbox Code Playgroud)
  1. 安装脚本。
  2. 打开时间轴页面
  3. 打开问题页面
  4. 单击一个投票按钮或最喜欢的星星。
  5. 您将看到时间线页面自动重新加载。

请注意,对于此特定示例,两个页面都在同一个域中(仅是为了使每个人都更容易运行演示脚本),但是代码/策略对于跨域页面也同样有效。

  • 对于其他无法使其工作的人:在消息中添加时间戳或其他更改内容很重要。 (2认同)