聚合辞典

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

当前为 2022-06-25 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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

// @match         *://www.merriam-webster.com/dictionary/*
// @match         *://www.ldoceonline.com/dictionary/*
// @match         *://dictionary.cambridge.org/dictionary/english/*
// @match         *://www.vocabulary.com/dictionary/*
// @match         *://www.collinsdictionary.com/dictionary/english/*
// @match         *://www.oxfordlearnersdictionaries.com/definition/english/*
// @match         *://www.britannica.com/dictionary/*
// @match         *://www.macmillandictionary.com/dictionary/british/*
// @match         *://www.etymonline.com/search?q=*
// @match         *://cn.bing.com/dict/search?q=*
// @match         *://www.urbandictionary.com/define.php?term=*
// @match         *://en.wikipedia.org/wiki/*
// @match         *://www.voicetube.com/definition/*
// @match         *://dict.youdao.com/w/*
// @match         *://dict.eudic.net/dicts/en/*
// @match         *://fanyi.sogou.com/text?keyword=*
// @match         *://www.jukuu.com/search.php?q=*
// @match         *://www.91dict.com/words*
// @match         *://www.zdic.net/hans/*
// @match         *://www.moedict.tw/*

// @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: "Longman",
		dicUrl: "https://www.ldoceonline.com/dictionary/",
		keyName: "",
		testUrl: /https:\/\/www.ldoceonline.com\/dictionary\/*/,
	},
	{
		name: "Cambridge",
		dicUrl: "https://dictionary.cambridge.org/dictionary/english/",
		keyName: "",
		testUrl: /https:\/\/dictionary.cambridge.org\/dictionary\/english\/*/,
	},
	{
		name: "Collins",
		dicUrl: "https://www.collinsdictionary.com/dictionary/english/",
		keyName: "",
		testUrl: /https:\/\/www.collinsdictionary.com\/dictionary\/english\/*/,
	},
	{
		name: "Oxford",
		dicUrl: "https://www.oxfordlearnersdictionaries.com/definition/english/",
		keyName: "",
		testUrl: /https:\/\/www.oxfordlearnersdictionaries.com\/definition\/english\/*/,
	},
	{
		name: "Merriam-Webster",
		dicUrl: "https://www.merriam-webster.com/dictionary/",
		keyName: "",
		testUrl: /https:\/\/www.merriam-webster.com\/dictionary\/*/,
	},
	{
		name: "Britannica",
		dicUrl: "https://www.britannica.com/dictionary/",
		keyName: "",
		testUrl: /https:\/\/www.britannica.com\/dictionary\/*/,
	},
	{
		name: "Macmillan",
		dicUrl: "https://www.macmillandictionary.com/dictionary/british/",
		keyName: "",
		testUrl: /https:\/\/www.macmillandictionary.com\/dictionary\/british\/*/,
	},
	{
		name: "Etymology",
		dicUrl: "https://www.etymonline.com/search?q=",
		keyName: "q",
		testUrl: /https:\/\/www.etymonline.com\/search.*/,
	},
	{
		name: "Bing",
		dicUrl: "https://cn.bing.com/dict/search?q=",
		keyName: "q",
		testUrl: /https:\/\/cn.bing.com\/dict\/search.*/,
	},
	{
		name: "Urban",
		dicUrl: "https://www.urbandictionary.com/define.php?term=",
		keyName: "term",
		testUrl: /https:\/\/www.urbandictionary.com\/define.php.*/,
	},
	{
		name: "Wikipedia",
		dicUrl: "https://en.wikipedia.org/wiki/",
		keyName: "",
		testUrl: /https:\/\/en.wikipedia.org\/wiki\/*/,
	},
	{
		name: "VoiceTube",
		dicUrl: "https://www.voicetube.com/definition/",
		keyName: "",
		testUrl: /https:\/\/www.voicetube.com\/definition\/*/,
	},
	{
		name: "人人词典",
		dicUrl: "https://www.91dict.com/words?w=",
		keyName: "w",
		testUrl: /https:\/\/www.91dict.com\/words.*/,
	},
	{
		name: "有道词典",
		dicUrl: "https://dict.youdao.com/w/",
		keyName: "",
		testUrl: /https:\/\/dict.youdao.com\/w\/*/,
	},
	{
		name: "搜狗翻译",
		dicUrl: "https://fanyi.sogou.com/text?keyword=",
		keyName: "keyword",
		testUrl: /https:\/\/fanyi.sogou.com\/text.*/,
	},
	{
		name: "欧路词典",
		dicUrl: "https://dict.eudic.net/dicts/en/",
		keyName: "",
		testUrl: /https:\/\/dict.eudic.net\/dicts\/en\/*/,
	},
	{
		name: "句酷",
		dicUrl: "http://www.jukuu.com/search.php?q=",
		keyName: "q",
		testUrl: /http:\/\/www.jukuu.com\/search.php.*/,
	},
	{
		name: "汉典",
		dicUrl: "https://www.zdic.net/hans/",
		keyName: "",
		testUrl: /https:\/\/www.zdic.net\/hans\/*/,
	},
	{
		name: "国语辞典/两岸辞典",
		dicUrl: "https://www.moedict.tw/",
		keyName: "",
		testUrl: /https:\/\/www.moedict.tw\/*/,
	},
];

// 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) {
		if (item.testUrl.test(window.location.href)) {
			keywords = getQueryVariable(item.keyName);
			break;
		}
	}
	console.log(keywords);
	return keywords;
}

// 添加节点
function addBox() {
	// 主元素
	const dics = document.createElement("div");
	dics.id = "dic-app-box";
	dics.style = `
	    position: fixed !important;
		top: 120px !important;
		left: 12px !important;
		width: 78px;
		background-color: hsla(200, 40%, 96%, .8);
		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;
		color: hsla(211, 60%, 35%, .8);
		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;`;
	dics.appendChild(title);
	
	// 辞典列表
	for (let index in urlMapping) {
		let item = urlMapping[index];
		
		// 列表样式
		let style = `
			display: block;
			color: hsla(211, 60%, 35%, .8) !important;
			padding: 6px 8px;
			text-decoration: none;`;
		let defaultStyle = style + `
		    color: hsla(211, 60%, 35%, .8) !important;`;
		let hoverStyle = style + `
			background-color: hsla(211, 60%, 35%, .1);`;
			
		// 辞典链接
		let a = document.createElement("a");
		a.innerText = item.name;
		a.style = defaultStyle;
		a.className = "dic-a";
		a.href = item.dicUrl + getKeywords();
		
		// 鼠标移入&移出效果,相当于 hover
		a.onmouseenter = function() {
			this.style = hoverStyle;
		};
		a.onmouseleave = function() {
			this.style = defaultStyle;
		};
		dics.appendChild(a);
	}
}

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