根据当前网页的域名自动在 Greasy Fork 上搜索脚本,并显示可移动的按钮,支持设置语言限制和搜索引擎
目前為
// ==UserScript==
// @name 自动搜索油叉脚本
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 根据当前网页的域名自动在 Greasy Fork 上搜索脚本,并显示可移动的按钮,支持设置语言限制和搜索引擎
// @author Your Name
// @license MIT
// @match *://*/*
// @icon https://www.google.com/s2/favicons?domain=greasyfork.org
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
// 获取当前网页的域名
var domain = window.location.hostname;
// 获取按钮显示设置
var showButton = GM_getValue('showButton', true);
// 获取用户设置
var useLanguageRestriction = GM_getValue('useLanguageRestriction', false);
var selectedSearchEngine = GM_getValue('selectedSearchEngine', 'greasyfork');
// 创建按钮
var button = document.createElement('div');
button.style.position = 'fixed';
button.style.bottom = '10px';
button.style.right = '10px';
button.style.width = '100px';
button.style.height = '30px';
button.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
button.style.color = 'white';
button.style.textAlign = 'center';
button.style.lineHeight = '30px';
button.style.borderRadius = '5px';
button.style.cursor = 'pointer';
button.style.zIndex = '9999';
button.innerText = '搜索脚本';
button.style.display = showButton ? 'block' : 'none';
// 鼠标悬停和离开效果
button.onmouseover = function() {
button.style.backgroundColor = 'rgba(0, 0, 0, 1)';
};
button.onmouseout = function() {
button.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
};
let isDragging = false;
// 按钮点击事件
button.onclick = function() {
if (!isDragging) {
var searchURL;
if (selectedSearchEngine === 'greasyfork') {
searchURL = useLanguageRestriction
? 'https://greasyfork.org/zh-CN/scripts/by-site/' + domain
: 'https://greasyfork.org/en/scripts/by-site/' + domain;
} else if (selectedSearchEngine === 'google') {
searchURL = 'https://www.google.com/search?q=site:greasyfork.org ' + domain + ' userscript';
}
GM_openInTab(searchURL, { active: true });
}
};
// 使按钮可移动
button.onmousedown = function(event) {
isDragging = false;
var shiftX = event.clientX - button.getBoundingClientRect().left;
var shiftY = event.clientY - button.getBoundingClientRect().top;
function moveAt(pageX, pageY) {
isDragging = true;
button.style.left = pageX - shiftX + 'px';
button.style.top = pageY - shiftY + 'px';
}
function onMouseMove(event) {
moveAt(event.pageX, event.pageY);
}
document.addEventListener('mousemove', onMouseMove);
button.onmouseup = function() {
document.removeEventListener('mousemove', onMouseMove);
button.onmouseup = null;
};
};
button.ondragstart = function() {
return false;
};
document.body.appendChild(button);
// 注册菜单命令
GM_registerMenuCommand('显示/隐藏搜索按钮', function() {
showButton = !showButton;
button.style.display = showButton ? 'block' : 'none';
GM_setValue('showButton', showButton);
});
GM_registerMenuCommand('启用/禁用语言限制', function() {
useLanguageRestriction = !useLanguageRestriction;
GM_setValue('useLanguageRestriction', useLanguageRestriction);
alert('语言限制已' + (useLanguageRestriction ? '启用' : '禁用'));
});
GM_registerMenuCommand('选择搜索引擎', function() {
var searchEngine = prompt('输入搜索引擎 (greasyfork 或 google):', selectedSearchEngine);
if (searchEngine === 'greasyfork' || searchEngine === 'google') {
selectedSearchEngine = searchEngine;
GM_setValue('selectedSearchEngine', selectedSearchEngine);
alert('搜索引擎已设置为 ' + selectedSearchEngine);
} else {
alert('无效的搜索引擎');
}
});
})();