【问题标题】:In Tampermonkey using @require with jQuery doesn't work在 Tampermonkey 中使用 @require 和 jQuery 不起作用
【发布时间】:2021-07-29 12:50:33
【问题描述】:

在我的一生中,我无法让它工作。

我正在尝试使用 Tampermonkey 编写一个用户脚本,它是 @require'ing 两个 jQuery 资源 - jQuery 和 jQuery UI。

每次,chrome 的控制台都会给我两到四个错误,它们看起来都是这样的:

core-c30de8a3f5468380db0b.js:1 GET about:blank net::ERR_UNKNOWN_URL_SCHEME

当我删除两个 @require 时,这些错误就会消失。我已经尝试过使用和不使用 window.jQ、http 和 https,直接从 jQuery 和 Google ajax 版本中提取,我已经尝试在文档启动和文档空闲时运行,我已经尝试了所有方法。脚本的所有其余部分都可以工作,但 requires 没有,显然 jQuery 位也没有。

这是我目前所拥有的一切:

// ==UserScript==
// @name         Score Graph
// @namespace    https://github.com/storjak
// @version      0.1
// @description  Score Graph
// @author       storjak
// @match        https://www.twitch.tv/*
// @grant        none
// @run-at       @run-at document-start
// @require      http://code.jquery.com/jquery-3.6.0.min.js
// @require      http://code.jquery.com/ui/1.12.1/jquery-ui.min.js

// ==/UserScript==

window.jQ = $;

(function() {
    $(document).ready(function() {
        $(function() {
            $("#container")
                .draggable({
                    handle: "#grab",
                    iframeFix: true,
                    containment: "document"
                });
        });
        appendButton();
    });

})();

// \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/ \/  All this stuff works and can be ignored

function appendButton() {
    let chartStatus = false;
    let removedElement;
    let root = document.getElementById("root");
    const newButton = document.createElement("div");
    newButton.innerHTML = '<button style="padding-left: 10px; padding-right: 10px; margin-right: 10px" class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonSecondary-sc-1qn4ixc-2 kMLrvA tw-core-button">Score Chart</button>';
    newButton.id = "score-graph-button";
    newButton.onclick = function scoreGraph() {
        if (chartStatus === false) {
            if (removedElement === undefined) {
                const src = "http://localhost:3000/userscript/" + location.pathname.slice(1);
                let newElement = document.createElement("div");
                newElement.style.cssText = "position: absolute; z-index: 9999; top: 100px; right: 10px; border: solid 1px black; border-radius: 8px; resize: both; overflow: hidden; width: 300px; height: 300px; display: flex; flex-direction: column;";
                newElement.id = "score-graph-container";
                newElement.innerHTML = '<div id="grab" style="background-color: #6441a5; border-bottom: solid 1px black; text-align: center; width: 100%; font-family: Arial, Helvetica, sans-serif; padding: 4px; cursor: move;">Drag Me!</div><div id="iframe-wrapper" style="height: 100%; width: 100%;"><iframe id="embed" style="height: 100%; width: 100%; border: none;" title="Score Graph" src=' + src + '></iframe></div>';
                root.append(newElement);
            } else {
                root.append(removedElement);
            }
            chartStatus = true;
        } else if (chartStatus === true) {
            removedElement = document.getElementById("score-graph-container").remove();
            chartStatus = false;
        }
    }
    const navBar = document.getElementsByClassName("tw-align-items-center tw-flex tw-flex-grow-1 tw-flex-shrink-1 tw-full-width tw-justify-content-end");
    navBar[0].append(newButton);
}

我已经被困在这个问题上好几天了,我快要失去理智了。

【问题讨论】:

    标签: javascript jquery require tampermonkey


    【解决方案1】:

    经过测试,完美运行。 require 可以正常工作,也许是其他 EXT 或某些东西阻塞了?

    不管怎样,把它修好了一点,这样拖动也能正常工作。

    // ==UserScript==
    // @name         New Userscript
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        https://www.twitch.tv/*
    // @icon         https://www.google.com/s2/favicons?domain=stackoverflow.com
    // @require      http://code.jquery.com/jquery-3.6.0.min.js
    // @require      http://code.jquery.com/ui/1.12.1/jquery-ui.min.js
    // @grant        none
    // ==/UserScript==
    
    function appendButton() {
        let chartStatus = false;
        let removedElement;
        let root = document.getElementById("root");
        const newButton = document.createElement("div");
        newButton.innerHTML = '<button style="padding-left: 10px; padding-right: 10px; margin-right: 10px" class="ScCoreButton-sc-1qn4ixc-0 ScCoreButtonSecondary-sc-1qn4ixc-2 kMLrvA tw-core-button">Score Chart</button>';
        newButton.id = "score-graph-button";
        newButton.onclick = function scoreGraph() {
            if (chartStatus === false) {
                if (removedElement === undefined) {
                    const src = "http://localhost:3000/userscript/" + location.pathname.slice(1);
                    let newElement = document.createElement("div");
                    newElement.style.cssText = "position: absolute; z-index: 9999; top: 100px; right: 10px; border: solid 1px black; border-radius: 8px; resize: both; overflow: hidden; width: 300px; height: 300px; display: flex; flex-direction: column;";
                    newElement.id = "score-graph-container";
                    newElement.innerHTML = '<div id="grab" style="background-color: #6441a5; border-bottom: solid 1px black; text-align: center; width: 100%; font-family: Arial, Helvetica, sans-serif; padding: 4px; cursor: move;">Drag Me!</div><div id="iframe-wrapper" style="height: 100%; width: 100%;"><iframe id="embed" style="height: 100%; width: 100%; border: none;" title="Score Graph" src=' + src + '></iframe></div>';
                    root.append(newElement);
                } else {
                    root.append(removedElement);
                }
                chartStatus = true;
            } else if (chartStatus === true) {
                removedElement = document.getElementById("score-graph-container").remove();
                chartStatus = false;
            }
        }
        const navBar = document.getElementsByClassName("tw-align-items-center tw-flex tw-flex-grow-1 tw-flex-shrink-1 tw-full-width tw-justify-content-end");
        navBar[0].append(newButton);
    }
    
    
    (function() {
    
    
        $(document).ready(function() {
            appendButton();
            $('body').on('click', '#grab', () => {
                $('#score-graph-container').draggable({
                    handle: '#grab',
                    iframeFix: true,
                    containment: 'document'
                });
            });
        });
    })();
    

    【讨论】:

    • 我不知道您使用了哪种魔法,但您的版本运行良好,而且我没有更改任何内容,我只是将您的版本复制粘贴到我的脚本文件中。只是JS的东西,我猜。无论如何,非常感谢你。
    猜你喜欢
    • 2013-06-19
    • 1970-01-01
    • 2018-07-31
    • 2023-03-11
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多