我试图用Tampermonkey运行此脚本,但出现错误:
eslint:no-undef-未定义$
”
我在做什么错?
脚本:
(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);
}
})();
最佳答案
您需要先定义全局变量。只需在顶部添加以下行:
/* globals jQuery, $, waitForKeyElements */
并且不要忘记在==UserScript==
行之间要求使用jquery:// @require http://code.jquery.com/jquery-3.x-git.min.js
关于error-handling - eslint : no-undef - `$` is not defined",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62282642/