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.
not defined: In JavaScript, it is one of the reference errors that JavaScript will throw when someone accesses the variable which is not inside the memory heap.
Summary. In JavaScript, a variable can be either defined or not defined, as well as initialized or uninitialized. typeof myVar === 'undefined' evaluates to true if myVar is not defined, but also defined and uninitialized. That's a quick way to determine if a variable is defined.
Reference errors in Javascript are mainly thrown when an attempt is made to reference a variable that does not exist or is out of scope. Therefore, in the majority of cases, a ReferenceError can be fixed by making sure that the referenced variable is defined correctly and is being called in the correct scope.
The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading.
Get the jQuery from window object
var $ = window.jQuery;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With