|
|

楼主 |
发表于 2026-5-7 12:12
|
显示全部楼层
- // 菜单ID常量
- const MENU_ID_JD = "search_jd";
- const MENU_ID_TAOBAO = "search_taobao";
- const MENU_ID_BAIDU = "search_baidu";
- const MENU_ID_BING = "search_bing";
- const MENU_ID_GOOGLE = "search_google";
- const MENU_ID_bilibili = "bilibili";
- const MENU_ID_百度翻译 = "百度翻译";
- const MENU_ID_IP = "IP";
- const MENU_ID_ip138 = "ip138";
- const MENU_ID_ip138_v6 = "ip138_v6";
- const MENU_ID_github = "github";
- const MENU_ID_youtube = "youtube";
- const MENU_ID_音乐搜索 = "音乐搜索";
- // 安装时创建右键菜单
- chrome.runtime.onInstalled.addListener(() => {
- // 先清空旧菜单
- chrome.contextMenus.removeAll();
- // 京东
- chrome.contextMenus.create({
- id: MENU_ID_JD,
- title: "京东",
- contexts: ["selection"] // 只在选中文字时显示
- });
- // 淘宝
- chrome.contextMenus.create({
- id: MENU_ID_TAOBAO,
- title: "淘宝",
- contexts: ["selection"] // 只在选中文字时显示
- });
- // 百度搜索
- chrome.contextMenus.create({
- id: MENU_ID_BAIDU,
- title: "百度",
- contexts: ["selection"] // 只在选中文字时显示
- });
- // 必应搜索
- chrome.contextMenus.create({
- id: MENU_ID_BING,
- title: "必应",
- contexts: ["selection"]
- });
- // 谷歌搜索
- chrome.contextMenus.create({
- id: MENU_ID_GOOGLE,
- title: "谷歌",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_bilibili,
- title: "bilibili",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_百度翻译,
- title: "百度翻译",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_IP,
- title: "IP",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_ip138,
- title: "ip138",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_ip138_v6,
- title: "ip138_v6",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_github,
- title: "github",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_youtube,
- title: "youtube",
- contexts: ["selection"]
- });
-
- chrome.contextMenus.create({
- id: MENU_ID_音乐搜索,
- title: "音乐搜索",
- contexts: ["selection"]
- });
-
-
-
-
- });
- // 点击菜单触发搜索
- chrome.contextMenus.onClicked.addListener((info) => {
- // 重点:只有 ip138_v6 不转义,其他全部正常转义
- let searchText;
- if (info.menuItemId === MENU_ID_ip138_v6) {
- searchText = info.selectionText; // 不转义
- } else {
- searchText = encodeURIComponent(info.selectionText); // 正常转义
- }
-
- let url = "";
- switch (info.menuItemId) {
- case MENU_ID_JD:
- url = `http://search.jd.com/Search?keyword=${searchText}&enc=utf-8`;
- break;
- case MENU_ID_TAOBAO:
- url = `https://s.taobao.com/search?q=${searchText}`;
- break;
- case MENU_ID_BAIDU:
- url = `https://www.baidu.com/s?wd=${searchText}`;
- break;
- case MENU_ID_BING:
- url = `https://www.bing.com/search?q=${searchText}`;
- break;
- case MENU_ID_GOOGLE:
- url = `https://www.google.com/search?q=${searchText}`;
- break;
- case MENU_ID_bilibili:
- url = `https://search.bilibili.com/all?keyword=${searchText}`;
- break;
- case MENU_ID_百度翻译:
- url = `https://fanyi.baidu.com/#en/zh/${searchText}`;
- break;
- case MENU_ID_IP:
- url = `https://www.ryzl.com.cn/ip/ip.php?ip=${searchText}`;
- break;
- case MENU_ID_ip138:
- url = `https://ip138.com/iplookup.php?ip=${searchText}&action=2`;
- break;
- case MENU_ID_ip138_v6:
- url = `https://www.ipshudi.com/${searchText}.htm`;
- break;
- case MENU_ID_github:
- url = `https://github.com/search?q=${searchText}`;
- break;
- case MENU_ID_youtube:
- url = `https://www.youtube.com/results?search_query=${searchText}`;
- break;
- case MENU_ID_音乐搜索:
- url = `https://tools.liumingye.cn/music/#/search/M/song/${searchText}`;
- break;
-
-
-
-
- }
- // 新标签打开搜索页
- if (url) chrome.tabs.create({ url });
- });
复制代码 |
|