diff --git a/scripts/advanced-search/background.js b/scripts/advanced-search/background.js
index efddd99..9f43f67 100644
--- a/scripts/advanced-search/background.js
+++ b/scripts/advanced-search/background.js
@@ -59,17 +59,19 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "[百度] " + text, description: "使用 [百度] 搜索 " + text + "", deletable: false },
- { content: "[搜狗] " + text, description: "使用 [搜狗] 搜索 " + text + "", deletable: false },
- { content: "[必应] " + text, description: "使用 [必应] 搜索 " + text + "", deletable: false },
- { content: "[360] " + text, description: "使用 [360] 搜索 " + text + "", deletable: false },
- { content: "[微博] " + text, description: "使用 [微博] 搜索 " + text + "", deletable: false },
- { content: "[知乎] " + text, description: "使用 [知乎] 搜索 " + text + "", deletable: false },
- { content: "[今日头条] " + text, description: "使用 [今日头条] 搜索 " + text + "", deletable: false },
- { content: "[中国搜索] " + text, description: "使用 [中国搜索] 搜索 " + text + "", deletable: false },
+ { content: "[百度] " + text, description: "使用 [百度] 搜索 " + encodeText + "", deletable: false },
+ { content: "[搜狗] " + text, description: "使用 [搜狗] 搜索 " + encodeText + "", deletable: false },
+ { content: "[必应] " + text, description: "使用 [必应] 搜索 " + encodeText + "", deletable: false },
+ { content: "[360] " + text, description: "使用 [360] 搜索 " + encodeText + "", deletable: false },
+ { content: "[微博] " + text, description: "使用 [微博] 搜索 " + encodeText + "", deletable: false },
+ { content: "[知乎] " + text, description: "使用 [知乎] 搜索 " + encodeText + "", deletable: false },
+ { content: "[今日头条] " + text, description: "使用 [今日头条] 搜索 " + encodeText + "", deletable: false },
+ { content: "[中国搜索] " + text, description: "使用 [中国搜索] 搜索 " + encodeText + "", deletable: false },
]);
return;
@@ -154,16 +156,18 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "img: [百度] " + text, description: "使用 [百度图片] 搜索 " + text + "", deletable: false },
- { content: "img: [搜狗] " + text, description: "使用 [搜狗图片] 搜索 " + text + "", deletable: false },
- { content: "img: [必应] " + text, description: "使用 [必应图片] 搜索 " + text + "", deletable: false },
- { content: "img: [360] " + text, description: "使用 [360图片] 搜索 " + text + "", deletable: false },
- { content: "img: [微博] " + text, description: "使用 [微博图片] 搜索 " + text + "", deletable: false },
- { content: "img: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + text + "", deletable: false },
- { content: "img: [中国搜索] " + text, description: "使用 [中国搜索图片] 搜索 " + text + "", deletable: false },
+ { content: "img: [百度] " + text, description: "使用 [百度图片] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [搜狗] " + text, description: "使用 [搜狗图片] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [必应] " + text, description: "使用 [必应图片] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [360] " + text, description: "使用 [360图片] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [微博] " + text, description: "使用 [微博图片] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + encodeText + "", deletable: false },
+ { content: "img: [中国搜索] " + text, description: "使用 [中国搜索图片] 搜索 " + encodeText + "", deletable: false },
]);
return;
},
@@ -221,26 +225,28 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "video: [B站] " + text, description: "使用 [哔哩哔哩动画] 搜索 " + text + "", deletable: false },
- { content: "video: [爱奇艺] " + text, description: "使用 [爱奇艺] 搜索 " + text + "", deletable: false },
- { content: "video: [腾讯视频] " + text, description: "使用 [腾讯视频] 搜索 " + text + "", deletable: false },
- { content: "video: [优酷] " + text, description: "使用 [优酷] 搜索 " + text + "", deletable: false },
- { content: "video: [百度] " + text, description: "使用 [百度视频] 搜索 " + text + "", deletable: false },
- { content: "video: [搜狗] " + text, description: "使用 [搜狗视频] 搜索 " + text + "", deletable: false },
- { content: "video: [微博] " + text, description: "使用 [微博视频] 搜索 " + text + "", deletable: false },
- { content: "video: [抖音] " + text, description: "使用 [抖音] 搜索 " + text + "", deletable: false },
- { content: "video: [必应] " + text, description: "使用 [必应视频] 搜索 " + text + "", deletable: false },
+ { content: "video: [B站] " + text, description: "使用 [哔哩哔哩动画] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [爱奇艺] " + text, description: "使用 [爱奇艺] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [腾讯视频] " + text, description: "使用 [腾讯视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [优酷] " + text, description: "使用 [优酷] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [百度] " + text, description: "使用 [百度视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [搜狗] " + text, description: "使用 [搜狗视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [微博] " + text, description: "使用 [微博视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [抖音] " + text, description: "使用 [抖音] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [必应] " + text, description: "使用 [必应视频] 搜索 " + encodeText + "", deletable: false },
// 以下内容超出9个不被显示
- { content: "video: [360] " + text, description: "使用 [360视频] 搜索 " + text + "", deletable: false },
- { content: "video: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + text + "", deletable: false },
- { content: "video: [快手] " + text, description: "使用 [快手] 搜索 " + text + "", deletable: false },
- { content: "video: [知乎] " + text, description: "使用 [知乎] 搜索 " + text + "", deletable: false },
- { content: "video: [搜狐] " + text, description: "使用 [搜狐视频] 搜索 " + text + "", deletable: false },
- { content: "video: [央视网] " + text, description: "使用 [央视网] 搜索 " + text + "", deletable: false },
- { content: "video: [中国搜索] " + text, description: "使用 [中国搜索视频] 搜索 " + text + "", deletable: false },
+ { content: "video: [360] " + text, description: "使用 [360视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [快手] " + text, description: "使用 [快手] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [知乎] " + text, description: "使用 [知乎] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [搜狐] " + text, description: "使用 [搜狐视频] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [央视网] " + text, description: "使用 [央视网] 搜索 " + encodeText + "", deletable: false },
+ { content: "video: [中国搜索] " + text, description: "使用 [中国搜索视频] 搜索 " + encodeText + "", deletable: false },
]);
return;
},
@@ -324,16 +330,18 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "news: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + text + "", deletable: false },
- { content: "news: [百度] " + text, description: "使用 [百度资讯] 搜索 " + text + "", deletable: false },
- { content: "news: [360] " + text, description: "使用 [360资讯] 搜索 " + text + "", deletable: false },
- { content: "news: [微博] " + text, description: "使用 [微博] 搜索 " + text + "", deletable: false },
- { content: "news: [人民网] " + text, description: "使用 [人民网] 搜索 " + text + "", deletable: false },
- { content: "news: [中国搜索] " + text, description: "使用 [中国搜索] 搜索 " + text + "", deletable: false },
- { content: "news: [快资讯] " + text, description: "使用 [快资讯] 搜索 " + text + "", deletable: false },
+ { content: "news: [今日头条] " + text, description: "使用 [今日头条] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [百度] " + text, description: "使用 [百度资讯] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [360] " + text, description: "使用 [360资讯] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [微博] " + text, description: "使用 [微博] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [人民网] " + text, description: "使用 [人民网] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [中国搜索] " + text, description: "使用 [中国搜索] 搜索 " + encodeText + "", deletable: false },
+ { content: "news: [快资讯] " + text, description: "使用 [快资讯] 搜索 " + encodeText + "", deletable: false },
]);
return;
},
@@ -390,22 +398,24 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "fanyi: [有道翻译] " + text, description: "翻译 | 使用 [有道翻译] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [百度] " + text, description: "翻译 | 使用 [百度翻译] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [腾讯] " + text, description: "翻译 | 使用 [腾讯翻译君] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [DeepL] " + text, description: "翻译 | 使用 [DeepL翻译] 翻译 " + text + "", deletable: false },
- // 【需要注入自动翻译】 { content: "fanyi: [海词翻译] " + text, description: "翻译 | 使用 [海词翻译] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [必应] " + text, description: "查词 | 使用 [必应词典] 查词 " + text + "", deletable: false },
- { content: "fanyi: [有道] " + text, description: "查词 | 使用 [有道] 查词 " + text + "", deletable: false },
- { content: "fanyi: [海词] " + text, description: "查词 | 使用 [海词] 查词 " + text + "", deletable: false },
- { content: "fanyi: [金山词霸] " + text, description: "查词 | 使用 [金山词霸] 查词 " + text + "", deletable: false },
+ { content: "fanyi: [有道翻译] " + text, description: "翻译 | 使用 [有道翻译] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [百度] " + text, description: "翻译 | 使用 [百度翻译] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [腾讯] " + text, description: "翻译 | 使用 [腾讯翻译君] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [DeepL] " + text, description: "翻译 | 使用 [DeepL翻译] 翻译 " + encodeText + "", deletable: false },
+ // 【需要注入自动翻译】 { content: "fanyi: [海词翻译] " + text, description: "翻译 | 使用 [海词翻译] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [必应] " + text, description: "查词 | 使用 [必应词典] 查词 " + encodeText + "", deletable: false },
+ { content: "fanyi: [有道] " + text, description: "查词 | 使用 [有道] 查词 " + encodeText + "", deletable: false },
+ { content: "fanyi: [海词] " + text, description: "查词 | 使用 [海词] 查词 " + encodeText + "", deletable: false },
+ { content: "fanyi: [金山词霸] " + text, description: "查词 | 使用 [金山词霸] 查词 " + encodeText + "", deletable: false },
// 以下内容超出9个不被显示
- { content: "fanyi: [360] " + text, description: "翻译 | 使用 [360翻译] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [翻译狗] " + text, description: "翻译 | 使用 [翻译狗] 翻译 " + text + "", deletable: false },
- { content: "fanyi: [Google] " + text, description: "翻译 | 使用 [Google翻译] 翻译 " + text + " (Google翻译在中国大陆无法使用)", deletable: false },
+ { content: "fanyi: [360] " + text, description: "翻译 | 使用 [360翻译] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [翻译狗] " + text, description: "翻译 | 使用 [翻译狗] 翻译 " + encodeText + "", deletable: false },
+ { content: "fanyi: [Google] " + text, description: "翻译 | 使用 [Google翻译] 翻译 " + encodeText + " (Google翻译在中国大陆无法使用)", deletable: false },
]);
return;
},
@@ -481,16 +491,18 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "paper: [知网] " + text, description: "使用 [中国知网] 搜索 " + text + "", deletable: false },
- { content: "paper: [万方] " + text, description: "使用 [万方数据] 搜索 " + text + "", deletable: false },
- { content: "paper: [维普] " + text, description: "使用 [维普期刊] 搜索 " + text + "", deletable: false },
- { content: "paper: [百度] " + text, description: "使用 [百度学术] 搜索 " + text + "", deletable: false },
- { content: "paper: [必应] " + text, description: "使用 [必应学术] 搜索 " + text + "", deletable: false },
- { content: "paper: [搜狗] " + text, description: "使用 [搜狗学术] 搜索 " + text + "", deletable: false },
- { content: "paper: [谷歌] " + text, description: "使用 [谷歌学术] 搜索 " + text + " (谷歌学术在中国大陆无法使用)", deletable: false },
+ { content: "paper: [知网] " + text, description: "使用 [中国知网] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [万方] " + text, description: "使用 [万方数据] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [维普] " + text, description: "使用 [维普期刊] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [百度] " + text, description: "使用 [百度学术] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [必应] " + text, description: "使用 [必应学术] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [搜狗] " + text, description: "使用 [搜狗学术] 搜索 " + encodeText + "", deletable: false },
+ { content: "paper: [谷歌] " + text, description: "使用 [谷歌学术] 搜索 " + encodeText + " (谷歌学术在中国大陆无法使用)", deletable: false },
]);
return;
},
@@ -549,12 +561,14 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "baike: [百度] " + text, description: "使用 [百度百科] 搜索 " + text + "", deletable: false },
- { content: "baike: [搜狗] " + text, description: "使用 [搜狗百科] 搜索 " + text + "", deletable: false },
- { content: "baike: [360] " + text, description: "使用 [360百科] 搜索 " + text + "", deletable: false },
+ { content: "baike: [百度] " + text, description: "使用 [百度百科] 搜索 " + encodeText + "", deletable: false },
+ { content: "baike: [搜狗] " + text, description: "使用 [搜狗百科] 搜索 " + encodeText + "", deletable: false },
+ { content: "baike: [360] " + text, description: "使用 [360百科] 搜索 " + encodeText + "", deletable: false },
]);
return;
},
@@ -619,14 +633,16 @@ var omniboxSearchModes = [
},
// 搜索建议
getSuggestions: async function (text, suggest) {
+ // 传入的 text 没有经过 encode
+ let encodeText = encodeXML(text)
// 如果前面已经有了 【[xx] 】,则先去掉
text = text.replace(/^\[.*?\]\s*/, "");
suggest([
- { content: "map: [百度] " + text, description: "使用 [百度地图] 搜索 " + text + "", deletable: false },
- { content: "map: [高德] " + text, description: "使用 [高德地图] 搜索 " + text + "", deletable: false },
- { content: "map: [必应] " + text, description: "使用 [必应地图] 搜索 " + text + "", deletable: false },
- { content: "map: [360] " + text, description: "使用 [360地图] 搜索 " + text + "", deletable: false },
- { content: "map: [搜狗] " + text, description: "使用 [搜狗地图] 搜索 " + text + "", deletable: false },
+ { content: "map: [百度] " + text, description: "使用 [百度地图] 搜索 " + encodeText + "", deletable: false },
+ { content: "map: [高德] " + text, description: "使用 [高德地图] 搜索 " + encodeText + "", deletable: false },
+ { content: "map: [必应] " + text, description: "使用 [必应地图] 搜索 " + encodeText + "", deletable: false },
+ { content: "map: [360] " + text, description: "使用 [360地图] 搜索 " + encodeText + "", deletable: false },
+ { content: "map: [搜狗] " + text, description: "使用 [搜狗地图] 搜索 " + encodeText + "", deletable: false },
]);
return;
},
@@ -678,15 +694,17 @@ var omniboxSearchModes = [
// },
// // 搜索建议
// getSuggestions: async function (text, suggest) {
+ // // 传入的 text 没有经过 encode
+ // let encodeText = encodeXML(text)
// // 如果前面已经有了 【[xx] 】,则先去掉
// text = text.replace(/^\[.*?\]\s*/, "");
// suggest([
// { content: "jk: [免责声明] " + text, description: "[免责声明] 小墨助手仅提供快捷搜索功能,不对搜索结果承担责任。搜索结果仅供参考,请自行甄别,以免上当受骗。继续搜索代表您已知晓此声明。", deletable: false },
- // { content: "jk: [丁香医生] " + text, description: "使用 [丁香医生] 搜索 " + text + "", deletable: false },
- // { content: "jk: [360] " + text, description: "使用 [360良医] 搜索 " + text + "", deletable: false },
- // { content: "jk: [好大夫] " + text, description: "使用 [好大夫在线] 搜索 " + text + "", deletable: false },
- // { content: "jk: [寻医问药] " + text, description: "使用 [寻医问药网] 搜索 " + text + "", deletable: false },
- // { content: "jk: [新华健康] " + text, description: "使用 [新华健康] 搜索 " + text + "", deletable: false },
+ // { content: "jk: [丁香医生] " + text, description: "使用 [丁香医生] 搜索 " + encodeText + "", deletable: false },
+ // { content: "jk: [360] " + text, description: "使用 [360良医] 搜索 " + encodeText + "", deletable: false },
+ // { content: "jk: [好大夫] " + text, description: "使用 [好大夫在线] 搜索 " + encodeText + "", deletable: false },
+ // { content: "jk: [寻医问药] " + text, description: "使用 [寻医问药网] 搜索 " + encodeText + "", deletable: false },
+ // { content: "jk: [新华健康] " + text, description: "使用 [新华健康] 搜索 " + encodeText + "", deletable: false },
// // 腾讯医典没有网页版;中华网健康没有搜索功能:https://health.china.com/;搜狐健康搜索为全站搜索:https://health.sohu.com/
// ]);
// return;
@@ -899,7 +917,7 @@ chrome.omnibox.onInputChanged.addListener(async function (text, suggest) {
// 访问后端服务获得搜索建议
var currentSearchMode = omniboxSearchModes[currentSearchModeIndex];
- currentSearchMode.getSuggestions(currentSearchMode.getInputText(text), suggest);
+ currentSearchMode.getSuggestions(currentSearchMode.getInputText(text, false), suggest);
});
/**
@@ -916,7 +934,7 @@ chrome.omnibox.onInputEntered.addListener(async function (text) {
updateDefaultSuggestion(text);
var searchMode = omniboxSearchModes[currentSearchModeIndex];
- var searchText = searchMode.getInputText(text);
+ var searchText = searchMode.getInputText(text, false);
searchMode.search(searchText);
console.log("用户输入:" + text);
});
@@ -964,7 +982,7 @@ async function checkIsActived() {
* @param string str
* @returns
*/
-function encodeXML(str) {
+function encodeXML(str, opt = { encodeAngleBrackets: true /* 是否转义 < > */ }) {
let result = str
let map = {
"&": "&",
@@ -973,6 +991,12 @@ function encodeXML(str) {
"\"": """,
"'": "'"
}
+
+ if (opt && !opt.encodeAngleBrackets) {
+ delete map["<"]
+ delete map[">"]
+ }
+
Object.keys(map).forEach((key) => result = result.replaceAll(key, map[key]))
return result
@@ -987,6 +1011,26 @@ function encodeXML(str) {
}
+/**
+ * 特殊字符 & < > " ' 的反转义(上述函数的反向操作)
+ *
+ * @param string str
+ * @returns
+ */
+function decodeXML(str) {
+ let result = str
+ let map = {
+ "&": "&",
+ "<": "<",
+ ">": ">",
+ "\"": """,
+ "'": "'"
+ }
+ Object.keys(map).forEach((key) => result = result.replaceAll(map[key], key))
+ return result
+}
+
+
/**
* 将当前标签页导航到指定Url / 或者新建标签页
*
@@ -1059,18 +1103,18 @@ function updateDefaultSuggestion(text) {
description.push(' ] ');
if (text.trim().length != 0) {
- description[2] = isPlaintext ? ('' + omniboxSearchModes[0].showText + ':' + text.trim() + '') : ('' + omniboxSearchModes[0].showText + '');
+ description[2] = isPlaintext ? ('' + omniboxSearchModes[0].showText + ':' + encodeXML(text.trim()) + '') : ('' + omniboxSearchModes[0].showText + '');
} else {
// 用户什么也没输入时,就高亮显示文字搜索关键字
description[2] = '' + omniboxSearchModes[0].showText + '';
}
- console.log("[更新下拉框提示]",
+ console.log("[更新搜索建议]",
+ "搜索模式:", omniboxSearchModes[currentSearchModeIndex].showText,
+ "isPlaintext", isPlaintext,
"text:", text,
- "匹配的搜索模式:", omniboxSearchModes[currentSearchModeIndex].showText);
- console.log(" isPlaintext:", isPlaintext);
- // console.log(description.join(''));
- console.log("[更新下拉框提示结束]");
+ // "description:", description,
+ )
chrome.omnibox.setDefaultSuggestion({
description: description.join('')