Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eslint: no-undef - `$` is not defined"

Im trying to run this script with Tampermonkey but i get Error: eslint: no-undef - $ is not defined" What im doing wrong?

Script:

(function() {
    'use strict';

    restore();

    $('span.tag').each((n, span) => {
        let input = document.querySelector(`input#${span.getAttribute('for')}`);
        span.addEventListener('click', () => {
            store(`tags`, input.value, input.checked);
        });
    });
    $('#board, #title').change((e) => {
        store(e.target.id, e.target.value);
    });

    function restore(){
        let json = localStorage.getItem('create') || "{}";
        let data = JSON.parse(json);

        Object.keys(data).forEach(key => {
            switch(key){
                case "tags":
                    data[key].forEach(val => {
                        document.querySelector(`input[value="${val}"]`).setAttribute('checked', 'checked');
                        document.querySelector(`span[for="create_${val}"]`).classList.add('selected');
                    });
                    break;
                case "board":
                    document.querySelector(`#board option[value="${data[key]}"]`).setAttribute('selected', 'selected');
                default:
                    document.querySelector(`#${key}`).value = data[key];
                    break;
            }
        });
    }

    function store(key, value, checked) {
        let json = localStorage.getItem('create') || "{}";
        let data = JSON.parse(json);

        if(arguments.length === 3){
            data[key] = new Set(data[key]);
            if(checked) {
                data[key].add(value);
            } else {
                data[key].delete(value);
            }

            data[key] = Array.from(data[key]);
        } else {
            data[key] = value;
        }

        json = JSON.stringify(data);
        localStorage.setItem('create', json);
    }
})();
like image 898
Mark B Avatar asked Oct 18 '25 09:10

Mark B


1 Answers

You need to define global variables before everything. Just include this line at the top:

/* globals jQuery, $, waitForKeyElements */

And don't forget to require jquery between the ==UserScript== lines:

// @require      http://code.jquery.com/jquery-3.x-git.min.js
like image 93
Pigmalijonas Avatar answered Oct 21 '25 22:10

Pigmalijonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!