通用搜索框架 - 用于多个网站的自动外部资源搜索
一个用于多个网站的自动外部资源搜索的油猴脚本,支持在多个网站上自动搜索相关资源并显示结果。
Jsoso.js 文件的全部内容脚本默认支持以下搜索网站:
脚本在以下网站上自动运行:
要添加新的搜索网站,需要在 manager.addSite() 中添加新的 Site 实例:
manager.addSite(new Site(
'网站名称', // 1. 网站名称(用于显示和配置)
id => `搜索URL`, // 2. 搜索URL生成函数
(response, id, previewData) => { // 3. 响应处理函数
// 解析响应,判断是否有搜索结果
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, "text/html");
// 根据网站结构解析结果
const hasResult = doc.querySelector("some-selector");
if (hasResult) {
previewData.url = `结果URL`; // 设置结果URL
}
},
'bg-blue-500' // 4. 按钮颜色类
));
response:HTTP响应对象id:搜索的IDpreviewData:结果对象,需设置 previewData.urlbg-blue-500)new Site(
name,
searchUrlFunc,
responseHandler,
buttonColor,
queryInterval = 1, // 查询间隔(毫秒),默认1
maxConcurrentSearches = 3 // 最大并发搜索数,默认3
)
要适配新的目标网站,需要完成以下步骤:
@match 规则// ==UserScript==
// @match *://*.example.com/*
// ==/UserScript==
创建一个函数来提取页面上的ID和容器元素:
function example_site_wall() {
const items = []
const items_ = document.querySelectorAll('选择器') // 选择包含ID的元素
items_.forEach(item => {
// 提取ID
const id = item.textContent.trim(); // 或使用其他方式提取
// 找到要插入按钮的容器
const element = item.parentElement;
items.push({
id,
element
})
})
return items
}
startSearch() 函数中添加调用function startSearch() {
manager.SearchItems = [];
manager.SearchItems.push(...FC2PPVDB_wall());
manager.SearchItems.push(...FC2PPVDB_singlepage());
manager.SearchItems.push(...javdb_wall());
manager.SearchItems.push(...javdb_singlepage());
manager.SearchItems.push(...FC2_wall());
manager.SearchItems.push(...FC2_singlepage());
manager.SearchItems.push(...FC2_ranking());
manager.SearchItems.push(...javbus_wall());
// 添加新网站
manager.SearchItems.push(...example_site_wall());
console.log('准备开始搜索', manager.SearchItems);
manager.initializeSearch();
FC2DB_removeNoImage()
}
格式化ID,移除 "FC2-" 前缀:
const id = Utils.formatID("FC2-123456"); // 返回 "123456"
创建结果容器:
const container = Utils.createDiv();
element.appendChild(container);
创建搜索按钮:
const button = Utils.createButton(
"https://example.com",
"搜索",
"bg-blue-500"
);
可用的颜色类:
bg-blue-500 - 蓝色bg-green-500 - 绿色bg-red-500 - 红色bg-purple-500 - 紫色bg-yellow-500 - 黄色bg-orange-500 - 橙色bg-gray-500 - 灰色脚本使用 GM.setValue/GM.getValue 实现缓存,缓存默认有效期为10天。
await Utils.cacheResult(cacheKey, previewData, expiryDay);
const cachedResult = await Utils.getCachedResult(cacheKey);
console.log('准备开始搜索', manager.SearchItems);GM_xmlhttpRequest、GM.setValue、GM.getValue 权限本项目仅供学习和个人使用。
sexjpg
欢迎提交 Issue 和 Pull Request!