$ is not defined Javascript

我怕爱的太早我们不能终老 提交于 2020-04-08 08:59:09

问题


I'm writing a piece of code currently in tampermonkey and I can't work out why i get this error in the console of google chrome,"Execution of script 'PalaceBOT' failed! $ is not defined", I have another script that uses the same principals and I do not experience these issues.

Script:

// ==UserScript==
// @name         SupremeBOT
// @namespace
// @version      0.1
// @description
// @author       @alfiefogg_
// @match        http://www.supremenewyork.com/shop/*
// @exclude      http://wwww.supremenewyork.com/shop/cart
// @require      https://gist.github.com/raw/2625891/waitForKeyElements.js
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
// @grant        none
// ==/UserScript==
var mySize = "large"; //Change to appropriate size
var productSort = "accessories"; //Change to appropriate size
(function() {
    var articles = $(".product-grid-item clearfix");
    if(productSort != "all"){
        for(var i = 0; i < articles.length;i++)
        {
            var category = $(articles[i]).find("a").attr("href");
            if(category.indexOf(productSort) == -1){
                articles[i].remove();
                document.getElementsByClassName("product-grid-item clearfix")[4].click();
            }
        }
    }
    waitForKeyElements("#img-main", exe);
})();
function exe(){
    selectSize();
    goCheckout();
}
function goCheckout(){
    var x = document.getElementById("add-remove-buttons");
    var z = x.getElementsByClassName("button")[0];

    if(z.className != "button remove"){
        z.click();
        setTimeout(goCheckout ,100);
    }else{
        window.location = "https://www.supremenewyork.com/checkout";
    }
}
function selectSize(){
    var sizeObj = document.getElementById("size");
    for(var i=0,sL=sizeObj.length;i<sL;i++){
        if(sizeObj.options[i].text == mySize){
            sizeObj.selectedIndex = i;
            break;
        }
    }
}

Do bear in mind that this is not a finished script.


回答1:


Get the jQuery from window object

var $ = window.jQuery;



回答2:


I didn't find a better answer other than this.

In general, this chunk of code defines the library for further usage inside tampermonkey editor.

/*globals MY_LIB*/ using this will take off all warnings.

For more information check this (https://jshint.com/docs/#inline-configuration). It explains what globals are, and how do they work.




回答3:


You need to include JQuery $ is not part of regular javascript



来源:https://stackoverflow.com/questions/43924769/is-not-defined-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!