标题笔记

将选中文字添加到网页标题最前面,以便在浏览器标签栏快速识别

目前為 2018-06-17 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        标题笔记
// @version     0.1.0.0
// @icon        https://tampermonkey.net/favicon.ico
// @description 将选中文字添加到网页标题最前面,以便在浏览器标签栏快速识别
// @author      You!
// @grant       unsafeWindow
// @include     *
// @run-at      document-start
// @namespace   https://greasyfork.org/zh-CN/scripts/369580-网页标题笔记
// ==/UserScript==

(function() {
	document.addEventListener('keydown', function() {
		if (192 !== event.keyCode) return;

		if (event.ctrlKey && !event.altKey && !event.shiftKey) setTitleNote(); //替换添加:Ctrl
		else if (!event.ctrlKey && event.altKey && !event.shiftKey) setTitleNote(true); //正向添加:Alt
		else if (event.ctrlKey && event.altKey && !event.shiftKey) setTitleNote(false); //反向添加:Ctrl + Alt
		else if (event.ctrlKey && !event.altKey && event.shiftKey) clearTitleNote(); //清除笔记:Ctrl + Shift

		function getSelectionTxt() {
			var txt = unsafeWindow.getSelection().toString().trim();
			return txt.length ? txt : null;
		}
		function getNote(txt, append) {
			if ('boolean' !== typeof append) {
				unsafeWindow.txts = [txt];
				return txt;
			} else {
				if (undefined === unsafeWindow.txts) unsafeWindow.txts = [];
				if (append) unsafeWindow.txts.push(txt);
				else unsafeWindow.txts.unshift(txt);
			}
			return unsafeWindow.txts.join('|');
		}
		function getOriginalTitle() {
			if (undefined === unsafeWindow.title0) unsafeWindow.title0 = document.title;
			return unsafeWindow.title0;
		}
		function setTitleNote(append) {
			var txt = getSelectionTxt();
			if (!txt) return;
			document.title = getNote(txt, append) + '▶' + getOriginalTitle();
		}
		function clearTitleNote() {
			if (undefined !== unsafeWindow.title0) document.title = unsafeWindow.title0;
		}
	});
}());