新增可更换 搜索引擎 功能

pull/387/head
jingjingxyk 2 years ago
parent 502f62c128
commit 3f74d11ebf

@ -12,37 +12,37 @@ let styleConfig = () => {
let customElement = () => { let customElement = () => {
let link = document.createElement("link"); let link = document.createElement("link");
link.setAttribute( link.setAttribute(
"href", "href",
chrome.runtime.getURL("web-components/x-custom-box.html") chrome.runtime.getURL("web-components/x-custom-box.html")
); );
document.head.appendChild(link); document.head.appendChild(link);
}; };
let getIframe = () => { let getIframe = () => {
let box = document.querySelector( let box = document.querySelector(
"#chinese-programmer-wrong-pronunciation-custom-iframe-box" "#chinese-programmer-wrong-pronunciation-custom-iframe-box"
); );
let iframe = null; let iframe = null;
if (!box) { if (!box) {
let custom_box = document.createElement( let custom_box = document.createElement(
"x-chinese-programmer-wrong-pronunciation-custom-box" "x-chinese-programmer-wrong-pronunciation-custom-box"
); );
let aside = document.createElement("aside"); let aside = document.createElement("aside");
aside.setAttribute( aside.setAttribute(
"id", "id",
"chinese-programmer-wrong-pronunciation-custom-iframe-box" "chinese-programmer-wrong-pronunciation-custom-iframe-box"
); );
aside.setAttribute( aside.setAttribute(
"class", "class",
"chinese-programmer-wrong-pronunciation-custom-iframe-box" "chinese-programmer-wrong-pronunciation-custom-iframe-box"
); );
aside.setAttribute("draggable", "true"); aside.setAttribute("draggable", "true");
let tool_bar = document.createElement("div"); let tool_bar = document.createElement("div");
tool_bar.setAttribute( tool_bar.setAttribute(
"class", "class",
"chinese-programmer-wrong-pronunciation-custom-tool-bar" "chinese-programmer-wrong-pronunciation-custom-tool-bar"
); );
tool_bar.addEventListener("click", (event) => { tool_bar.addEventListener("click", (event) => {
event.preventDefault(); event.preventDefault();
@ -52,16 +52,16 @@ let getIframe = () => {
let window_close_icon = document.createElement("span"); let window_close_icon = document.createElement("span");
window_close_icon.innerText = "❌"; window_close_icon.innerText = "❌";
window_close_icon.setAttribute( window_close_icon.setAttribute(
"class", "class",
"chinese-programmer-wrong-pronunciation-custom-window-close-icon" "chinese-programmer-wrong-pronunciation-custom-window-close-icon"
); );
window_close_icon.addEventListener("click", closeSearchWindow); window_close_icon.addEventListener("click", closeSearchWindow);
tool_bar.appendChild(window_close_icon); tool_bar.appendChild(window_close_icon);
iframe = document.createElement("iframe"); iframe = document.createElement("iframe");
iframe.setAttribute( iframe.setAttribute(
"id", "id",
"chinese-programmer-wrong-pronunciation-custom-iframe" "chinese-programmer-wrong-pronunciation-custom-iframe"
); );
iframe.setAttribute("security", "restricted"); iframe.setAttribute("security", "restricted");
//iframe.setAttribute('sandbox',"") //iframe.setAttribute('sandbox',"")
@ -77,18 +77,18 @@ let getIframe = () => {
showSetSearchEngine(tool_bar, window_close_icon); showSetSearchEngine(tool_bar, window_close_icon);
} else { } else {
if ( if (
hasClass( hasClass(
box, box,
"chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden" "chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden"
) )
) { ) {
removeClass( removeClass(
box, box,
"chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden" "chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden"
); );
} }
iframe = box.querySelector( iframe = box.querySelector(
"#chinese-programmer-wrong-pronunciation-custom-iframe" "#chinese-programmer-wrong-pronunciation-custom-iframe"
); );
iframe.setAttribute("src", "about:blank"); iframe.setAttribute("src", "about:blank");
//iframe.contentDocument.close() //iframe.contentDocument.close()
@ -96,8 +96,8 @@ let getIframe = () => {
iframe = document.createElement("iframe"); iframe = document.createElement("iframe");
iframe.setAttribute( iframe.setAttribute(
"id", "id",
"chinese-programmer-wrong-pronunciation-custom-iframe" "chinese-programmer-wrong-pronunciation-custom-iframe"
); );
iframe.setAttribute("security", "restricted"); iframe.setAttribute("security", "restricted");
box.appendChild(iframe); box.appendChild(iframe);
@ -115,10 +115,10 @@ let showSetSearchEngine = (box, window_close_icon) => {
{ {
let search_engin_provider = { let search_engin_provider = {
"goToYouDaoSearch": "有道", goToYouDaoSearch: "有道",
"goToGoogleSearch": "谷歌", goToGoogleSearch: "谷歌",
"goToBingDictSearch": "必应词典", goToBingDictSearch: "必应词典",
"goToBingSearch": "必应", goToBingSearch: "必应",
}; };
let select = document.createElement("select"); let select = document.createElement("select");
select.setAttribute("name", "search_engin_provider"); select.setAttribute("name", "search_engin_provider");
@ -139,8 +139,8 @@ let showSetSearchEngine = (box, window_close_icon) => {
} }
{ {
let search_engin_provider_tab = { let search_engin_provider_tab = {
"current_tab": "当前标签展示结果", current_tab: "当前标签展示结果",
"new_tab": "新标签展示结果", new_tab: "新标签展示结果",
}; };
let select = document.createElement("select"); let select = document.createElement("select");
select.setAttribute("class", "search_engin_provider_tab"); select.setAttribute("class", "search_engin_provider_tab");
@ -162,8 +162,8 @@ let showSetSearchEngine = (box, window_close_icon) => {
let div = document.createElement("span"); let div = document.createElement("span");
div.setAttribute( div.setAttribute(
"class", "class",
"chinese-programmer-wrong-pronunciation-custom-tool-bar-setup" "chinese-programmer-wrong-pronunciation-custom-tool-bar-setup"
); );
div.innerText = ``; div.innerText = ``;
// box.insertBefore(div,select) // box.insertBefore(div,select)
@ -172,23 +172,23 @@ let showSetSearchEngine = (box, window_close_icon) => {
//关闭窗口 //关闭窗口
let closeSearchWindow = () => { let closeSearchWindow = () => {
let box = document.querySelector( let box = document.querySelector(
"#chinese-programmer-wrong-pronunciation-custom-iframe-box" "#chinese-programmer-wrong-pronunciation-custom-iframe-box"
); );
console.log(box); console.log(box);
if ( if (
box && box &&
!hasClass( !hasClass(
box, box,
"chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden" "chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden"
) )
) { ) {
addClass( addClass(
box, box,
"chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden" "chinese-programmer-wrong-pronunciation-custom-iframe-box-hidden"
); );
} }
let iframe = box.querySelector( let iframe = box.querySelector(
"#chinese-programmer-wrong-pronunciation-custom-iframe" "#chinese-programmer-wrong-pronunciation-custom-iframe"
); );
iframe.setAttribute("src", "about:blank"); iframe.setAttribute("src", "about:blank");
}; };
@ -233,7 +233,7 @@ let setupConfig = (search_engine_name, tab) => {
let cleanOpener = () => { let cleanOpener = () => {
sessionStorage.removeItem(opener_key); sessionStorage.removeItem(opener_key);
console.log("重置扩展搜索结果打开方式配置---ok"); console.log("重置搜索结果打开方式配置---ok");
}; };
export { export {

@ -1,84 +1,84 @@
import box from "./box.js"; import box from "./box.js";
let init = () => { let init = () => {
let URLObj = new URL(location.href); let URLObj = new URL(location.href);
console.log(URLObj); console.log(URLObj);
if (document.querySelector("#readme table tbody")) { if (document.querySelector("#readme table tbody")) {
//载入自定义组件样式 //载入自定义组件样式
box.styleConfig(); box.styleConfig();
//载入自定义组件 //载入自定义组件
box.customElement(); box.customElement();
let audio_player = new Audio(); let audio_player = new Audio();
audio_player.setAttribute("autoplay", "true"); audio_player.setAttribute("autoplay", "true");
document document
.querySelector("#readme table tbody") .querySelector("#readme table tbody")
.addEventListener("click", (event) => { .addEventListener("click", (event) => {
//console.log(event.target) //console.log(event.target)
// console.log(event.target.nodeType) // console.log(event.target.nodeType)
// console.log(event.target.nodeName); // console.log(event.target.nodeName);
let parentElement = event.target.parentElement; let parentElement = event.target.parentElement;
if (parentElement && parentElement.nodeName === "TR") { if (parentElement && parentElement.nodeName === "TR") {
if (parentElement.firstElementChild === event.target) { if (parentElement.firstElementChild === event.target) {
//使用搜索引擎查询发音 //使用搜索引擎查询发音
box.goToSearchPronounce(event.target.innerText); box.goToSearchPronounce(event.target.innerText);
} }
} }
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
let audio_url = null; let audio_url = null;
if (event.target.nodeName === "TD") { if (event.target.nodeName === "TD") {
let aTag = event.target.querySelector("a"); let aTag = event.target.querySelector("a");
if (aTag) { if (aTag) {
audio_url = aTag.getAttribute("href"); audio_url = aTag.getAttribute("href");
} }
} }
if (event.target.nodeName === "IMG") { if (event.target.nodeName === "IMG") {
let aTag = event.target.parentNode.parentNode; let aTag = event.target.parentNode.parentNode;
audio_url = aTag.getAttribute("href"); audio_url = aTag.getAttribute("href");
} }
if (audio_url) { if (audio_url) {
let desURL = new URL(audio_url); let desURL = new URL(audio_url);
//console.log(desURL.protocol); //console.log(desURL.protocol);
if (desURL.protocol === "http:") { if (desURL.protocol === "http:") {
//skip http //skip http
location.href = audio_url; location.href = audio_url;
} else { } else {
// console.log("audio_url:", audio_url); // console.log("audio_url:", audio_url);
audio_player.setAttribute("src", audio_url); audio_player.setAttribute("src", audio_url);
} }
} }
}); });
document document
.querySelector("#readme table tbody") .querySelector("#readme table tbody")
.addEventListener("mouseover", (event) => { .addEventListener("mouseover", (event) => {
let parentElement = event.target.parentElement; let parentElement = event.target.parentElement;
if (parentElement && parentElement.nodeName === "TR") { if (parentElement && parentElement.nodeName === "TR") {
if (parentElement.firstElementChild === event.target) { if (parentElement.firstElementChild === event.target) {
event.target.setAttribute("title", "点击我打开搜索引擎检索"); event.target.setAttribute("title", "点击我打开搜索引擎检索");
event.target.style.cursor = "pointer"; event.target.style.cursor = "pointer";
} }
} }
}); });
let table = document.querySelector("#readme table"); let table = document.querySelector("#readme table");
let parent = table.parentNode; let parent = table.parentNode;
let note = document.createElement("span"); let note = document.createElement("span");
note.innerText = `⚪重置扩展提供的搜索结果打开方式⚪`; note.innerText = `⚪重置扩展提供的搜索结果打开方式⚪`;
note.setAttribute( note.setAttribute(
"class", "class",
"chinese-programmer-wrong-pronunciation-custom-note-reset" "chinese-programmer-wrong-pronunciation-custom-note-reset"
); );
note.addEventListener("click", (event) => { note.addEventListener("click", (event) => {
//重置配置 //重置配置
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
box.cleanOpener(); box.cleanOpener();
}); });
parent.insertBefore(note, table); parent.insertBefore(note, table);
} else { } else {
console.log("no found README.md table"); console.log("no found README.md table");
} }
}; };
export { init }; export { init };

@ -15,10 +15,9 @@ let goToBingSearch = (word) => {
return `https://cn.bing.com/search?q=how%20to%20pronounce%20${word}`; return `https://cn.bing.com/search?q=how%20to%20pronounce%20${word}`;
}; };
export { export {
goToYouDaoSearch, goToYouDaoSearch,
goToGoogleSearch, goToGoogleSearch,
goToBingDictSearch, goToBingDictSearch,
goToBingSearch goToBingSearch,
}; };

@ -18,7 +18,7 @@ function removeClass(el, className) {
function getCookie(name) { function getCookie(name) {
let arr, let arr,
reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if ((arr = document.cookie.match(reg))) { if ((arr = document.cookie.match(reg))) {
return decodeURIComponent(arr[2]); return decodeURIComponent(arr[2]);
} else { } else {
@ -33,16 +33,16 @@ function setCookie(name, value, second, domain) {
var exp = new Date(); var exp = new Date();
exp.setTime(exp.getTime() + second * 1000); exp.setTime(exp.getTime() + second * 1000);
document.cookie = document.cookie =
name + name +
"=" + "=" +
encodeURIComponent(value) + encodeURIComponent(value) +
";expires=" + ";expires=" +
exp.toGMTString() + exp.toGMTString() +
";path=" + ";path=" +
path + path +
";domain=" + ";domain=" +
domain + domain +
";SameSite=None;Secure"; ";SameSite=None;Secure";
} }
async function getCookies(domain) { async function getCookies(domain) {
let cookies = await cookieStore.getAll({ domain: domain }); let cookies = await cookieStore.getAll({ domain: domain });

Loading…
Cancel
Save