16

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.

Liam
  • 22,818
  • 25
  • 93
  • 157
A. Fogg
  • 189
  • 1
  • 1
  • 3

3 Answers3

27

Get the jQuery from window object

var $ = window.jQuery;
Smart Manoj
  • 3,837
  • 2
  • 24
  • 45
  • 1
    Adding this line to the top scope of the script worked for me. We just needed to get the jquery object out of the `window` where tampermonkey is putting it from the `// @require` (and alias it to `$`). Works like a charm, thanks! – George Pantazes Nov 09 '18 at 22:56
  • i wanted some way to mark it as recognized like in JSLint without needing to add extra code statements, just annotations. It already exists on the page. – Pysis May 09 '19 at 15:48
  • 2
    would you explain further? – Youssof H. Aug 10 '19 at 14:45
4

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.

Youssof H.
  • 665
  • 3
  • 10
  • 33
-4

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

MBurnham
  • 371
  • 1
  • 9