本文介绍了Tampermonkey jQuery需要不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改为Grease Monkey编写的一段代码,以使其与Tampermonkey兼容.尽管我的@require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js . require可在Greasemonkey上使用.

I am trying to modify a piece of code I wrote for Grease Monkey to make it compatible with Tampermonkey. Tamper monkey keeps saying '$' is not defined despite my @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js. The require works on Greasemonkey.

Tampermonkey安装的功能概述可以识别JQuery的需求.

The Tampermonkey instaled function(s) overview recognizes the JQuery require.

// ==UserScript==
// @name     Function
// @version  1
// @run-at   document-end
// @require  https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==

var userIP;

$.ajax({
    url: "https://api.ipify.org/?format=json", // Getting user Ip Address
    async: false,
    dataType: 'json',
    success: function(data) {
        userIP = data.ip; // Saving user Ip Address
    }
});

推荐答案

由于您说它仅在编辑器中,因此这可能是Tampermonkey的语法检查功能,它不会加载所需的脚本,并将其用作代码检查过程的一部分.因此,它只是看到变量没有在用户脚本本身的任何位置声明,并显示警告.该脚本仍应按预期工作.

Since you say it is only in the editor this is probably Tampermonkey's syntax checking not loading required scripts, and using them as part of the code checking process. So it just sees that a variable has not been declared anywhere in the user script itself and shows the warning. The script should still work as expected.

如果消息烦扰您,可以通过在脚本顶部显式声明$变量来清除它们,如下所示:

If the messages annoy you, you can clear them by explicitly declaring the $ variable at the top of your script like so:

var $ = window.jQuery;//OR
var $ = window.$;

这篇关于Tampermonkey jQuery需要不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 03:39
查看更多