From b52ee1e03f1eee742de8f244bf8767106f8d7686 Mon Sep 17 00:00:00 2001
From: Coding Zhang <2291200076@qq.com>
Date: Wed, 9 Feb 2022 13:05:59 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=B3=E4=BD=BF=E5=9C=A8?=
=?UTF-8?q?=E6=96=B0=E6=A0=87=E7=AD=BE=E9=A1=B5=E4=B9=9F=E6=96=B0=E5=BB=BA?=
=?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=B5=E8=BF=9B=E8=A1=8C=E6=90=9C=E7=B4=A2?=
=?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9B=E6=B7=BB=E5=8A=A0=E7=99=BE=E7=A7=91?=
=?UTF-8?q?=E5=88=86=E7=B1=BB=EF=BC=8C=E5=88=A0=E9=99=A4=E5=81=A5=E5=BA=B7?=
=?UTF-8?q?=E5=88=86=E7=B1=BB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../assets/js/advanced-search/background.js | 151 +++++++++++++-----
.../content-helper/baike.baidu.com.js | 27 ++++
packages/shell-chrome/manifest.json | 10 ++
3 files changed, 144 insertions(+), 44 deletions(-)
create mode 100644 packages/shell-chrome/assets/js/advanced-search/content-helper/baike.baidu.com.js
diff --git a/packages/shell-chrome/assets/js/advanced-search/background.js b/packages/shell-chrome/assets/js/advanced-search/background.js
index 84d7bf6..8c7c033 100644
--- a/packages/shell-chrome/assets/js/advanced-search/background.js
+++ b/packages/shell-chrome/assets/js/advanced-search/background.js
@@ -540,16 +540,16 @@ var omniboxSearchModes = [
},
// #############################################################################################################
{
- key: "jk",
+ key: "baike",
// 显示文字
- showText: "健康",
+ showText: "百科",
// 搜索模式匹配
match: function (text) {
- return /^jk( |:|\uff1a)?/i.test(text)
+ return /^baike( |:|\uff1a)?/i.test(text)
},
// 获取输入文字
getInputText: function (text, encodeText = true) {
- let returnText = /^jk(:| |\uff1a)?(.*)$/i.exec(text)[2].trim()
+ let returnText = /^baike(:| |\uff1a)?(.*)$/i.exec(text)[2].trim()
return encodeText ? encodeXML(returnText) : returnText
},
// 搜索建议
@@ -557,53 +557,119 @@ var omniboxSearchModes = [
// 如果前面已经有了 【[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 },
- // 腾讯医典没有网页版;中华网健康没有搜索功能:https://health.china.com/;搜狐健康搜索为全站搜索:https://health.sohu.com/
+ { content: "baike: [百度] " + text, description: "使用 [百度百科] 搜索 " + text + "", deletable: false },
+ { content: "baike: [搜狗] " + text, description: "使用 [搜狗百科] 搜索 " + text + "", deletable: false },
]);
return;
},
// 执行搜索
search: function (text) {
let searchInput = /^(\[.*?\])?( )?(.*)$/.exec(text)
- let searchType = /^\[(.*?)\]$/.exec((searchInput[1] ?? "[免责声明]"/* 默认弹出免责声明 */).trim())[0].trim()
+ let searchType = /^\[(.*?)\]$/.exec((searchInput[1] ?? "[百度]"/* 默认百度百科搜索 */).trim())[0].trim()
let searchText = searchInput[3].trim()
- console.log("[学术论文搜索开始]");
+ console.log("[百科搜索开始]");
console.log(" 传入参数为:", text);
console.log(" searchInput为:", searchInput);
console.log(" searchType为:", searchType);
console.log(" searchText为:", searchText);
- alert("[免责声明] 学生助手仅提供快捷搜索功能,不对搜索结果承担责任。搜索结果仅供参考,请自行甄别,以免上当受骗。继续搜索代表您已知晓此声明。");
switch (searchType) {
default:
- case "[免责声明]":
- // Silence is gold.
+ case "[百度]":
+ // 后面参数通过注入的js代码获取并在网页加载完后填入到搜索框中,点击搜索按钮
+ navigate("https://baike.baidu.com/?__bitdance_extension__=" + encodeURIComponent(searchText), true);
break;
- case "[丁香医生]":
- navigate("https://dxy.com/search/result?query=" + encodeURIComponent(searchText), true);
- break;
- case "[360]":
- navigate("https://ly.so.com/s?q=" + encodeURIComponent(searchText), true);
- break;
- case "[好大夫]":
- navigate("https://so.haodf.com/index/search?kw=" + encodeURIComponent(searchText), true);
- break;
- case "[寻医问药]":
- navigate("https://so.xywy.com/comse.php?keyword=" + encodeURIComponent(searchText), true);
- break;
- case "[新华健康]":
- navigate("http://so.xinhuanet.com/#search/0/" + encodeURIComponent(searchText) + "/1/", true);
+ case "[搜狗]":
+ //步骤一:创建异步对象
+ var ajax = new XMLHttpRequest();
+ //步骤二:设置请求的url参数,参数一是请求的类型,参数二是请求的url,可以带参数,动态的传递参数starName到服务端
+ ajax.open('get', 'https://baike.sogou.com/bapi/searchBarEnter?searchText=' + encodeURIComponent(searchText));
+ //步骤三:发送请求
+ ajax.send();
+ //步骤四:注册事件 onreadystatechange 状态改变就会调用
+ ajax.onreadystatechange = function () {
+ console.log("ajax result", ajax)
+ if (ajax.readyState == 4) {
+ if (ajax.status == 200) {
+ //步骤五 如果能够进到这个判断 说明 数据 完美的回来了,并且请求的页面是存在的
+ console.log(ajax.responseText);//输入相应的内容
+ navigate("https://baike.sogou.com" + ajax.responseText, true);
+ } else {
+ alert("搜索失败,可能是搜狗官网搜索相关api已变更,你的输入已经复制到剪切板,请手动粘贴搜索");
+ navigate("https://baike.sogou.com/", true);
+ }
+ }
+ }
break;
}
- console.log("[学术论文搜索结束]");
+ console.log("[百科搜索结束]");
}
},
// #############################################################################################################
// {
+ // key: "jk",
+ // // 显示文字
+ // showText: "健康",
+ // // 搜索模式匹配
+ // match: function (text) {
+ // return /^jk( |:|\uff1a)?/i.test(text)
+ // },
+ // // 获取输入文字
+ // getInputText: function (text, encodeText = true) {
+ // let returnText = /^jk(:| |\uff1a)?(.*)$/i.exec(text)[2].trim()
+ // return encodeText ? encodeXML(returnText) : returnText
+ // },
+ // // 搜索建议
+ // getSuggestions: async function (text, suggest) {
+ // // 如果前面已经有了 【[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 },
+ // // 腾讯医典没有网页版;中华网健康没有搜索功能:https://health.china.com/;搜狐健康搜索为全站搜索:https://health.sohu.com/
+ // ]);
+ // return;
+ // },
+ // // 执行搜索
+ // search: function (text) {
+ // let searchInput = /^(\[.*?\])?( )?(.*)$/.exec(text)
+ // let searchType = /^\[(.*?)\]$/.exec((searchInput[1] ?? "[免责声明]"/* 默认弹出免责声明 */).trim())[0].trim()
+ // let searchText = searchInput[3].trim()
+ // console.log("[学术论文搜索开始]");
+ // console.log(" 传入参数为:", text);
+ // console.log(" searchInput为:", searchInput);
+ // console.log(" searchType为:", searchType);
+ // console.log(" searchText为:", searchText);
+ // alert("[免责声明] 学生助手仅提供快捷搜索功能,不对搜索结果承担责任。搜索结果仅供参考,请自行甄别,以免上当受骗。继续搜索代表您已知晓此声明。");
+ // switch (searchType) {
+ // default:
+ // case "[免责声明]":
+ // // Silence is gold.
+ // break;
+ // case "[丁香医生]":
+ // navigate("https://dxy.com/search/result?query=" + encodeURIComponent(searchText), true);
+ // break;
+ // case "[360]":
+ // navigate("https://ly.so.com/s?q=" + encodeURIComponent(searchText), true);
+ // break;
+ // case "[好大夫]":
+ // navigate("https://so.haodf.com/index/search?kw=" + encodeURIComponent(searchText), true);
+ // break;
+ // case "[寻医问药]":
+ // navigate("https://so.xywy.com/comse.php?keyword=" + encodeURIComponent(searchText), true);
+ // break;
+ // case "[新华健康]":
+ // navigate("http://so.xinhuanet.com/#search/0/" + encodeURIComponent(searchText) + "/1/", true);
+ // break;
+ // }
+ // console.log("[学术论文搜索结束]");
+ // }
+ // },
+ // #############################################################################################################
+ // {
// key: "yn",
// // 显示文字
// showText: "网页内搜索(Todo)",
@@ -819,11 +885,11 @@ function encodeXML(str) {
* 将当前标签页导航到指定Url / 或者新建标签页
*
* @param String url 要导航到的url
- * @param bool openInNewTab 是否打开新标签页
+ * @param bool openInNewTab 是否打开新标签页 false - 当前标签页, true - 新标签页(当前标签页为newtab时使用当前标签页)
*/
function navigate(url, openInNewTab = false) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
- if (!openInNewTab || isCurrentNewTab()) {
+ if (!openInNewTab || isCurrentNewTab(tabs)) {
// 如果不在新标签页打开,或者当前标签页是新标签页
chrome.tabs.update(tabs[0].id, { url: url });
} else {
@@ -837,18 +903,15 @@ function navigate(url, openInNewTab = false) {
/**
* 获取当前是否是新标签页
+ * 需要先使用以下代码获取当前所有标签页
+ * chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { });
+ *
+ * @param {*} tabs
*/
-function isCurrentNewTab() {
- chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
- if (tabs && tabs.length > 0 && !!tabs[0].url && /^(.*?):\/\/newtab\/$/.test(tabs[0].url)) {
- console.log("当前标签页是新标签页");
- return true;
- }
- else {
- console.log("当前标签页不是新标签页");
- return false;
- }
- });
+function isCurrentNewTab(tabs) {
+ let isNewTab = (tabs && tabs.length > 0 && !!tabs[0].url && /^(.*?):\/\/newtab\/$/.test(tabs[0].url));
+ console.log("当前标签页" + (isNewTab ? "是" : "不是") + "新标签页");
+ return isNewTab;
}
diff --git a/packages/shell-chrome/assets/js/advanced-search/content-helper/baike.baidu.com.js b/packages/shell-chrome/assets/js/advanced-search/content-helper/baike.baidu.com.js
new file mode 100644
index 0000000..e7f9ae8
--- /dev/null
+++ b/packages/shell-chrome/assets/js/advanced-search/content-helper/baike.baidu.com.js
@@ -0,0 +1,27 @@
+console.log("[BitDance extension] 学生助手插件 - 百度百科自动搜索模块加载成功");
+
+// refer: https://www.cnblogs.com/chen-lhx/p/5198612.html
+$.extend({
+ getUrlVars: function () {
+ var vars = [], hash;
+ var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
+ for (var i = 0; i < hashes.length; i++) {
+ hash = hashes[i].split('=');
+ vars.push(hash[0]);
+ vars[hash[0]] = hash[1];
+ }
+ return vars;
+ },
+ getUrlVar: function (name) {
+ return $.getUrlVars()[name];
+ }
+});
+
+window.onload = () => {
+ let transText = $.getUrlVar('__bitdance_extension__');
+ if (!transText || transText.trim() == "") return
+ console.log("[BitDance extension] 学生助手插件 - 百度百科自动搜索模块 - 模块文本为:", transText);
+
+ document.getElementById("query").value = decodeURIComponent(transText)
+ document.getElementById("search").click()
+}
diff --git a/packages/shell-chrome/manifest.json b/packages/shell-chrome/manifest.json
index a9ec05a..16c52ef 100644
--- a/packages/shell-chrome/manifest.json
+++ b/packages/shell-chrome/manifest.json
@@ -87,6 +87,16 @@
"assets/js/advanced-search/content-helper/qikan.cqvip.com.js"
],
"run_at": "document_start"
+ },
+ {
+ "matches": [
+ "*://baike.baidu.com/*"
+ ],
+ "js": [
+ "assets/js/lib/jquery.min.js",
+ "assets/js/advanced-search/content-helper/baike.baidu.com.js"
+ ],
+ "run_at": "document_start"
}
],
"browser_action": {