[问卦] 要学油猴脚本语法有推荐的书吗?

楼主: powerg5 (mac)   2024-09-15 19:39:04
原本因为找不到让浏览器有便利贴的脚本语法,想自已写一个给chrome用
一开始怕直接写会失败还找了chatGPT和它边聊边写
结果还是失败…现在就想说干脆不要有省钱的心态
认真的找书来边看边学吧,可是,该挑哪一本才好?
希望给点建议吧?
底下顺便给出写失败的语法,这是chatGPT替我写的第三版了
目前问题出在按了对应的快速键(ctrl+shift+s)
或是在网页上直接单/双按鼠标左键或右键都无法开出写便利贴的选单或按钮
不懂怎么回事??
// ==UserScript==
// @name 高级随手贴
// @namespace http://tampermonkey.net/
// @version 0.3
// @description 增强的随手贴功能,支持搜寻和分类
// @author You
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// 创建随手贴按钮
const button = document.createElement('button');
button.textContent = '随手贴';
button.style.position = 'fixed';
button.style.top = '10px';
button.style.right = '10px';
button.style.padding = '10px';
button.style.backgroundColor = '#f0f0f0';
button.style.border = '1px solid #ccc';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
document.body.appendChild(button);
// 创建便条纸面板
const panel = document.createElement('div');
panel.style.position = 'fixed';
panel.style.top = '50px';
panel.style.right = '10px';
panel.style.width = '250px';
panel.style.maxHeight = '500px';
panel.style.backgroundColor = '#ffffe0';
panel.style.border = '1px solid #ccc';
panel.style.borderRadius = '5px';
panel.style.display = 'none';
panel.style.overflowY = 'auto';
panel.style.zIndex = '1000';
document.body.appendChild(panel);
// 创建搜寻框
const searchBox = document.createElement('input');
searchBox.placeholder = '搜寻便条';
searchBox.style.width = 'calc(100% - 20px)';
searchBox.style.margin = '10px';
searchBox.style.padding = '5px';
panel.appendChild(searchBox);
// 创建分类选择器
const categorySelect = document.createElement('select');
categorySelect.style.width = 'calc(100% - 20px)';
categorySelect.style.margin = '10px';
categorySelect.style.padding = '5px';
const defaultOption = document.createElement('option');
defaultOption.textContent = '所有分类';
defaultOption.value = '';
categorySelect.appendChild(defaultOption);
panel.appendChild(categorySelect);
// 创建便条纸容器
const notesContainer = document.createElement('div');
panel.appendChild(notesContainer);
// 显示或隐藏面板的函数
function togglePanel() {
panel.style.display = panel.style.display === 'none' ? 'block' :
'none';
if (panel.style.display === 'block') {
loadNotes();
}
}
// 绑定按钮点击事件
button.addEventListener('click', togglePanel);
// 键盘快捷键设置
document.addEventListener('keydown', (event) => {
// Ctrl + Shift + S
if (event.ctrlKey && event.shiftKey && event.key === 'S') {
togglePanel();
event.preventDefault(); // 防止默认行为(例如浏览器快捷键)
}
});
// 加载便条纸
function loadNotes() {
console.log('加载便条纸'); // 确认加载便条纸
notesContainer.innerHTML = '';
const categories = JSON.parse(localStorage.getItem('noteCategories')
|| '{}');
const notes = JSON.parse(localStorage.getItem('stickyNotes') || '[]');
const selectedCategory = categorySelect.value;
notes.forEach(note => {
if (selectedCategory === '' || note.category ===
selectedCategory) {
const noteElement = document.createElement('div');
noteElement.style.padding = '10px';
noteElement.style.border = '1px solid #ccc';
noteElement.style.borderRadius = '5px';
noteElement.style.marginBottom = '10px';
noteElement.style.backgroundColor = '#fff';
const title = document.createElement('h4');
title.textContent = note.title;
noteElement.appendChild(title);
const content = document.createElement('p');
content.textContent = note.content;
noteElement.appendChild(content);
notesContainer.appendChild(noteElement);
}
});
}
})();

Links booklink

Contact Us: admin [ a t ] ucptt.com