【问题标题】:tampermonkey script run accross multiple pagesTampermonkey 脚本跨多个页面运行
【发布时间】:2020-03-11 08:37:23
【问题描述】:

这是我想做的一个例子。 每当我们在目标 url 上时,即 stackoverflow 会在底部出现一个带有按钮的粘性页脚。 在搜索中键入内容的按钮之一,提交表单。在此之后,它等待页面加载并执行刚刚加载的页面,即点击第一个链接。

我发现在提交后单击是不可能的,因为页面的框架发生了变化或类似的事情,用tampermonkey怎么可能做到这一点。

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    $('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
    $('body').append(
        `<script type="text/javascript">
             function myFunction() {
                 // page 1
                 document.querySelector('#search > div > input').value="tampermonkey";
                 document.forms[0].submit();

                 // page 2 (DOEST WORK)
                 document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a');
             }
         </script>`
    );

    $('#test').css({'position': 'fixed',
                   'left': '0',
                   'bottom': '0',
                   'width': '100%',
                   'background-color': 'red',
                   'color': 'white',
                   'text-align': 'center',
                   'z-index':'1'
                  });

})();

【问题讨论】:

  • 问题是您提交并期望代码继续运行 - 您需要根据您所在的页面运行不同的代码
  • 没错,我在某个地方找到了可以使用 post message 之类的东西,但我不太明白怎么做。
  • postmessage 跨 iframes 工作...而不是跨不在浏览器中的页面 - document.forms[0].submit(); 用新页面替换当前页面...您无法从新页面与旧页面通信,因为旧页面不再在浏览器中 - 正如我所说,您需要根据页面运行不同的代码
  • 啊,好吧,所以这里唯一的选择是 tampermonkey 是为提交获得的任何 url 创建一个单独的脚本?
  • 可以是同一个猴子脚本——只需要在你的脚本中添加一些逻辑

标签: javascript tampermonkey


【解决方案1】:

您的脚本可能无法正常工作的一个原因是因为它位于脚本的顶部

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/
// @grant        none
// ==/UserScript==


// @match 仅匹配不包括搜索页面的 stackoverflow.com 主页,因此 @match 应该是匹配主页和搜索页面的 // @match https://stackoverflow.com/*。你也应该让它成为“点击我!”按钮仅通过使用if 语句显示在主页上。 如果你愿意,我也完全解决了你的问题。

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/*
// @grant        none
// ==/UserScript==

window.onload = function() {
    'use strict';
    if (location.href == "https://stackoverflow.com/search?q=tampermonkey") {
        alert(document.querySelector('#question-summary-29592068 > div.summary > div.result-link > h3 > a'));
    }else {
        $('body').append('<div id="test"><p><button onclick="myFunction()">Click me</button></p></div>');
        $('body').append(
            `<script type="text/javascript">
                 function myFunction() {
                     // page 1
                     document.querySelector('#search > div > input').value="tampermonkey";
                     document.forms[0].submit();
                 }
            </script>`
        );

        $('#test').css({
            'position': 'fixed',
            'left': '0',
            'bottom': '0',
            'width': '100%',
            'background-color': 'red',
            'color': 'white',
            'text-align': 'center',
            'z-index':'1'
        });
    }

};

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2022-01-11
    • 2016-11-02
    相关资源
    最近更新 更多