聚合辞典

在常用英文/中文在线辞典网页上增加一个浮层列表,以方便在其之间快速跳转,提高词汇查询效率。

目前為 2022-06-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          聚合辞典
// @namespace     http://tampermonkey.net/
// @version       0.1.9
// @description   在常用英文/中文在线辞典网页上增加一个浮层列表,以方便在其之间快速跳转,提高词汇查询效率。
// @author        http://twitter.com/rockucn
// @icon          https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org

// @match         *://www.vocabulary.com/dictionary/*
// @match         *://www.91dict.com/words*

// @grant         unsafeWindow
// @grant         window.onload
// @grant         GM_getValue
// @grant         GM_setValue
// @run-at        document-body

// @license       MIT
// ==/UserScript==

// 辞典网址配置
const urlMapping = [
	{
		name: "Vocabulary",
		dicUrl: "https://www.vocabulary.com/dictionary/",
		keyName: "",
		testUrl: /https:\/\/www.vocabulary.com\/dictionary\/*/,
	},
	{
		name: "人人词典",
		dicUrl: "https://www.91dict.com/words?w=",
		keyName: "w",
		testUrl: /https:\/\/www.91dict.com\/words.*/,
	},
];

// JS 获取 url 参数
function getQueryVariable(variable) {
	let query = window.location.search.substring(1);
	if (query === "") {
	    return decodeURIComponent(window.location.href.split("/").pop());
	} else {
    	let pairs = query.split("&");
    	for (let pair of pairs) {
    		let [key, value] = pair.split("=");
    		if (key === variable) {
    			return decodeURIComponent(value);
    		}
    	}
	}
	return null;
}

// 从 url 中获取搜索关键词
function getKeywords() {
	let keywords = "";
	for (let item of urlMapping) {
		// 如果关键词在 url 的末尾
		if (item.keyName === "") {
			keywords = window.location.href.split("/").pop();
			break;
		}
		// 如果关键词在 href 的 search 参数中
		if (item.testUrl.test(window.location.href)) {
			keywords = getQueryVariable(item.keyName);
			break;
		}
	}
	console.log(keywords);
	return keywords;
}

// 获取域名
const hostname = window.location.hostname;

let isBlank = GM_getValue("isBlank");

console.log("新标签页打开?", isBlank);
if (isBlank === undefined) {
	GM_setValue("isBlank", false);
	isBlank = false;
}

// 改变打开辞典链接的方式(当前tab中打开/新tab中打开)
const engine = document.getElementsByClassName("dic-a");
function triggerAttribute(value) {
	for (const item of engine) {
		item.target = value;
	}
}

// 添加节点
function addBox() {
	// 主元素
	const dics = document.createElement("div");
	dics.id = "dic-app-box";
	dics.style = `
	    position: fixed;
		top: 240px;
		left: 8px;
		width: 100px;
		background-color: rgba(230, 230, 230, 0.5);
		font-size: 12px;
		border-radius: 6px;
		z-index: 99999;`;
	document.body.insertAdjacentElement("afterBegin", dics);
	
	// 标题
	let title = document.createElement("span");
	title.innerText = "聚合辞典";
	title.style = `
		display: block;
		text-align: center;
		margin-top: 10px;
		margin-bottom: 5px;
		font-size: 12px;
		font-weight: bold;
		-webkit-user-select: none;
		-moz-user-select: none;
		-ms-user-select: none;
		user-select:none;`;
	title.style.textDecoration = isBlank ? "underline" : "";
	title.ondblclick = () => {
		title.style.textDecoration = !isBlank ? "underline" : "";
		GM_setValue("isBlank", !isBlank);
		isBlank = !isBlank;
		triggerAttribute(isBlank ? "_blank" : "");
	};
	dics.appendChild(title);
	
	// 辞典列表
	for (let index in urlMapping) {
		let item = urlMapping[index];
		
		// 列表样式
		let style = `
			display: block;
			padding: 8px;
			text-decoration: none`;
		let defaultStyle = style + `
			color: rgba(51, 51, 51, 0.6) !important;`;
		let hoverStyle = style + `
			color: #fff !important;
			background-color: #555;`;
			
		// 辞典链接
		let a = document.createElement("a");
		a.innerText = item.name;
		a.style = defaultStyle;
		a.className = "dic-a";
		a.href = item.dicUrl + getKeywords();
		if (!item.dicUrl.includes(hostname) && isBlank) {
			a.target = "_blank";
		}
		
		// 鼠标移入&移出效果,相当于 hover
		a.onmouseenter = function() {
			this.style = hoverStyle;
		};
		a.onmouseleave = function() {
			this.style = defaultStyle;
		};
		dics.appendChild(a);
	}
}

(function() {
	"use strict";
	window.onload = addBox();
})();