Greasy Fork

Greasy Fork is available in English.

简法主页功能增强

在简法主页上增加其他个性化设置

当前为 2021-10-19 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴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.12
// @description  在简法主页上增加其他个性化设置
// @author       那年那兔那些事
// @match        https://www.jianfast.com
// @match        https://www.jianfast.com/?pc=1
// @match        https://www.jianfast.com/m
// @icon         https://s3.bmp.ovh/imgs/2021/08/2a5feb8f5f886e70.png
// ==/UserScript==

(function() {
	//store为本地存储功能
	var store = {
		//set方法在localstorage中修改指定JSON数据
		set: function(key, val) {
			if (!val) {
				return;
			}
			try {
				var json = JSON.stringify(val);
				if (typeof JSON.parse(json) === "object") { // 验证一下是否为JSON字符串防止保存错误
					localStorage.setItem(key, json);
				}
			} catch (e) {
				return false;
			}
		},
		//get方法在localstorage中获取指定JSON数据
		get: function(key) {
			if (this.has(key)) {
				return JSON.parse(localStorage.getItem(key));
			}
		},
		//has方法在localstorage中查询指定JSON数据是否存在
		has: function(key) {
			if (localStorage.getItem(key)) {
				return true;
			} else {
				return false;
			}
		},
		//del方法在localstorage中删除指定JSON数据
		del: function(key) {
			localStorage.removeItem(key);
		}
	};
	//settings对象为设置项
	var settings = {
		settingsData: {
			searchEngine: true, //true:当前标签页打开搜索结果;false:新标签页打开搜索结果
			bookMarks: true, //true:当前标签页打开书签网页;false:新标签页打开书签网页
			settingsPurify: true, //true:启动设置页广告净化;false:关闭设置页广告净化
			hideRandBg: true //true:启动主页随机背景入口隐藏;false:关闭主页随机背景入口隐藏
		},
		//get方法获取settings对象属性
		get: function(key) {
			return this.settingsData[key];
		},
		//get方法获取settings对象所有属性
		getAll: function() {
			return this.settingsData;
		},
		//set方法设置settings对象属性
		set: function(key, value) {
			if (typeof value === "boolean") {
				this.settingsData[key] = value;
			} else {
				console.log("value错误");
			}
		},
		//initData方法初始化settings对象属性
		initData: function() {
			var localData = store.get("settingsData");
			var settingsData = this.settingsData;
			if (localData) {
				this.settingsData = {
					...settingsData,
					...localData
				};
			} else {
				store.set("settingsData", this.settingsData);
			}
		},
		//init方法初始化搜索引擎与书签的打开方式
		init: function() {
			this.initData();
			newSettingsPageFn.init();
			searchEngine.init();
			bookMarks.init();
			settingsPurify.init();
			hideRandBg.init();
			console.log("简法主页功能增强:初始化完成");
		},
		//monitor方法用于检错、监控修改结果
		monitor: function() {
			var Timer = setInterval(function() {
				searchEngine.monitor();
				bookMarks.monitor();
				settingsPurify.monitor();
				hideRandBg.monitor();
			}, 500);
			console.log("简法主页功能增强:检错程序启动(定时器ID:" + Timer + ")");
		},
		//on方法用于启动整个程序
		on: function() {
			console.log("简法主页功能增强:主程序启动");
			this.init();
			this.monitor();
		}
	}
	//searchEngine对象为搜索引擎项
	var searchEngine = {
		//change方法用于改变搜索按钮类型,从而便于覆盖搜索打开方式
		change: function() {
			var searchBtn = document.getElementById("search-btn");
			if (searchBtn) {
				searchBtn.type = "text";
			}
		},
		//click方法用于覆盖原搜索按钮点击方法
		click: function() {
			if (location.href.search("jianfast.com/m") === -1) {
				var searchBar = document.getElementById("search");
				var url = searchBar.getAttribute("data-engine-start");
				var val = searchBar.value;
				if (settings.get("searchEngine")) {
					location.href = url + val;
				} else {
					open(url + val);
				}
			}
		},
		//enter方法用于覆盖原回车搜索方法
		enter: function(event) {
			if (event.keyCode === 13) {
				searchEngine.click();
			}
		},
		//initSearch方法用于初始化搜索引擎,覆盖新方法
		initSearch: function() {
			searchEngine.change();
			var searchBtn = document.getElementById("search-btn");
			if (searchBtn) {
				searchBtn.onclick = this.click;
			}
			var searchBar = document.getElementById("search");
			if (searchBar) {
				searchBar.onkeydown = this.enter;
			}
		},
		//keywordClick方法用于覆盖原联想词点击方法
		keywordClick: function(target) {
			var searchBar = document.getElementById("search");
			searchBar.value = target.innerText;
			this.click();
		},
		//keywordOver方法用于覆盖原联想词鼠标事件(置于上方)方法
		keywordOver: function(target) {
			var targetPare = target.parentElement;
			if (targetPare && targetPare.childElementCount > 0) {
				for (let i = 0; i < targetPare.childElementCount; i++) {
					this.keywordLeave(targetPare.children[i]);
				}
			}
			target.style = "background-color: rgb(241, 241, 241);";
		},
		//keywordLeave方法用于覆盖原联想词鼠标事件(离开)方法
		keywordLeave: function(target) {
			target.style = "background-color: rgba(255, 255, 255, 0.3);";
		},
		//initKeyword方法用于初始化联想词,覆盖新方法
		initKeyword: function(keywordBox) {
			if (keywordBox) {
				keywordBox.innerHTML = keywordBox.innerHTML; //初始化,覆盖原方法
				var keyword = keywordBox.children;
				if (keyword.length > 0) {
					for (let i = 0; i < keyword.length; i++) {
						keyword[i].onmouseover = function() {
							searchEngine.keywordOver(keyword[i]);
						};
						keyword[i].onmouseleave = function() {
							searchEngine.keywordLeave(keyword[i]);
						};
						keyword[i].onclick = function() {
							searchEngine.keywordClick(keyword[i]);
						};
					}
				}
			}
		},
		//initSearch方法用于初始化搜索引擎,覆盖新方法
		init: function() {
			this.initSearch();
			this.initKeyword();
		},
		//monitor方法用于检错、监控修改结果,若出错则调用init方法重新覆盖
		monitor: function() {
			var searchForm = document.getElementById("search-form");
			var searchBar = document.getElementById("search");
			var searchBtn = document.getElementById("search-btn");
			if ((searchBar && searchBar.onkeydown === null) || (searchBtn && searchBtn.type !== "text") || (
					searchBtn && searchBtn.onclick === null)) {
				this.initSearch();
			}
			var keywordBox = document.getElementById("search-keyword-box");
			if (keywordBox && keywordBox.childElementCount > 0) {
				var keywordInitFlag = false;
				var keyword = keywordBox.children;
				for (let i = 0; i < keyword.length; i++) {
					if (keyword[i].onmouseover === null || keyword[i].onmouseleave === null || keyword[i]
						.onclick === null) {
						keywordInitFlag = true;
						break;
					}
				}
				if (keywordInitFlag) {
					this.initKeyword(keywordBox);
				}
			}
		}
	}
	//bookMarks对象为主页书签项
	var bookMarks = {
		//change方法用于改变书签打开方式
		change: function(Obj) {
			if (settings.get("bookMarks") && Obj.target !== "") {
				Obj.target = "";
			} else if (!settings.get("bookMarks") && Obj.target !== "_blank") {
				Obj.target = "_blank";
			}
		},
		//init方法用于遍历书签并调用change方法改变打开方式
		init: function() {
			var siteBox, aBox, aBoxLen;
			var idArray = ["site-box", "site-wrap"];
			for (let i = 0; i < idArray.length; i++) {
				siteBox = document.getElementById(idArray[i])
				if (siteBox) {
					break;
				}
			}
			if (siteBox && siteBox.childElementCount > 0) {
				for (let i = 0; i < siteBox.childElementCount; i++) {
					this.change(siteBox.children[i]);
				}
			}
		},
		//monitor方法用于检错程序
		monitor: function() {
			this.init();
		}
	}
	//settingsPurify为设置页净化功能
	var settingsPurify = {
		//init方法用于初始化净化广告
		init: function() {
			var adObj = document.getElementsByClassName("console-bottom-ad")[0];
			if (adObj) {
				if (settings.get("settingsPurify") && adObj.style.display !== "none") {
					adObj.style.display = "none";
				} else if (!settings.get("settingsPurify") && adObj.style.display === "none") {
					adObj.style.display = "";
				}
			}
			var bottomBtnBox = document.getElementById("console-bottom-btn-box");
			if (bottomBtnBox) {
				var bottomBtn = bottomBtnBox.children[1];
				if (bottomBtn) {
					if (settings.get("settingsPurify") && bottomBtn.innerText === "设为主页") {
						bottomBtn.innerText = "移动版";
						bottomBtn.href = "/m";
						bottomBtn.target = "";
					} else if (!settings.get("settingsPurify") && bottomBtn.innerText !== "设为主页") {
						bottomBtn.innerText = "设为主页";
						bottomBtn.href = "/zhuye";
						bottomBtn.target = "_blank";
					}
				}
				bottomBtn = bottomBtnBox.children[2];
				if (bottomBtn) {
					if (settings.get("settingsPurify") && bottomBtn.innerText === "关于") {
						bottomBtn.innerText = "问题反馈";
						bottomBtn.href = "/contact";
						bottomBtn.target = "_blank";
					} else if (!settings.get("settingsPurify") && bottomBtn.innerText !== "关于") {
						bottomBtn.innerText = "关于";
						bottomBtn.href = "/about";
						bottomBtn.target = "_blank";
					}
				}
			}
		},
		//monitor方法用于检错程序
		monitor: function() {
			this.init();
		}
	}
	//hideRandBg为主页随机背景入口隐藏功能
	var hideRandBg = {
		//init方法用于初始化入口隐藏
		init: function() {
			var randBgBtn = document.getElementById("rand-bg-btn");
			if (randBgBtn) {
				if (settings.get("hideRandBg") && randBgBtn.style.display !== "none") {
					randBgBtn.style.display = "none";
				} else if (!settings.get("hideRandBg") && randBgBtn.style.display === "none") {
					randBgBtn.style.display = "";
				}
			}
		},
		//monitor方法用于检错程序
		monitor: function() {
			this.init();
		}
	}
	//newSettingsPageFn为增强设置页
	var newSettingsPageFn = {
		optData: [{
			tittle: "搜索结果打开方式",
			value: "searchEngine",
			choice: [{
				t: "当前标签页",
				v: true
			}, {
				t: "新标签页",
				v: false
			}]
		}, {
			tittle: "主页书签打开方式",
			value: "bookMarks",
			choice: [{
				t: "当前标签页",
				v: true
			}, {
				t: "新标签页",
				v: false
			}]
		}, {
			tittle: "设置页广告净化",
			value: "settingsPurify",
			choice: [{
				t: "开启净化",
				v: true
			}, {
				t: "关闭净化",
				v: false
			}]
		}, {
			tittle: "随机背景入口隐藏",
			value: "hideRandBg",
			choice: [{
				t: "开启隐藏",
				v: true
			}, {
				t: "关闭隐藏",
				v: false
			}]
		}, {
			tittle: "关于脚本",
			type: "note",
			noteData: [{
				l: "当前版本",
				r: "Version0.12"
			}, {
				l: "版本更新",
				r: "<a href='http://greasyfork.icu/zh-CN/scripts/431279' style:'color:black'>GreasyFork</a>"
			}, {
				l: "反馈建议",
				r: "<a href='http://greasyfork.icu/zh-CN/scripts/431279/feedback' style:'color:black'>点此处反馈</a>"
			}]
		}],
		//clickFn方法为设置选项按钮的点击功能
		clickFn: function(id, key) {
			var Obj = document.getElementById(id);
			var setValue = settings.get(key);
			var optValue = Obj.value;
			if (setValue !== optValue) {
				settings.set(key, optValue);
				var newObj = Obj.parentElement.children;
				for (let i = 0; i < newObj.length; i++) {
					if (newObj[i].id === id) {
						newObj[i].style.border = "1px solid #2c7bf6";
						newObj[i].style.color = "#2c7bf6";
					} else {
						newObj[i].style.border = "1px solid rgba(0, 0, 0, 0.1)";
						newObj[i].style.color = "";
					}
				}
				saveFlag = true;
				var newSaveBox = document.getElementById("new-save-box");
				if (newSaveBox && newSaveBox.style.display === "none") {
					newSaveBox.style.display = "flex";
				}
				var tittleBox = document.getElementById("console-title-box");
				if (tittleBox && tittleBox.style.display !== "none") {
					tittleBox.style.display = "none";
				}
			}
		},
		//createTittle方法创建一个盛放设置标题的元素对象
		createTittle: function(val) {
			var Box = document.createElement("div");
			Box.setAttribute("class", "console-bigTitle");
			Box.innerText = val;
			return Box;
		},
		//createChoiceBtn方法创建一个设置选项按钮对象
		createChoiceBtn: function(choice, id) {
			var Btn = document.createElement("div");
			var BtnID = id + "-" + choice.v;
			var key = id.slice("moreSet-Opt-".length);
			Btn.style =
				"padding: 0 10px;width: 35%;margin: 5px 10px;height: 25px;box-sizing: border-box;line-height: 23px;text-align: center;border-radius: 100px;font-size: 13px;border: 1px solid rgba(0, 0, 0, 0.1);cursor: pointer;user-select: none;transition: all .3s;";
			Btn.innerText = choice.t;
			Btn.value = choice.v;
			Btn.id = BtnID;
			Btn.onclick = function() {
				newSettingsPageFn.clickFn(BtnID, key);
			};
			if (settings.get(key) === choice.v) {
				Btn.style.border = "1px solid #2c7bf6";
				Btn.style.color = "#2c7bf6";
			}
			return Btn;
		},
		//createChoice方法创建一个设置选项按钮对象的集合对象
		createChoice: function(value, choice) {
			var Box = document.createElement("div");
			var BoxID = "moreSet-Opt-" + value;
			Box.style =
				"width:100%;display:flex;justify-content:center;flex-flow:row wrap;margin-top:20px;";
			Box.id = BoxID
			var Btn;
			if (Array.isArray(choice)) {
				for (let i = 0; i < choice.length; i++) {
					Btn = this.createChoiceBtn(choice[i], BoxID);
					Box.appendChild(Btn);
				}
			} else {
				Btn = this.createChoiceBtn(choice, BoxID);
				Box.appendChild(Btn);
			}
			return Box;
		},
		//createOpt方法创建一个完整的设置项对象
		createOpt: function(tittle, value, choice) {
			var ResObj = false;
			if (tittle && value && choice) {
				ResObj = document.createElement("div");
				var newTittle = this.createTittle(tittle);
				var newChoice = this.createChoice(value, choice);
				ResObj.appendChild(newTittle);
				ResObj.appendChild(newChoice);
			}
			return ResObj;
		},
		createNoteText: function(data) {
			var textObj = document.createElement("div");
			textObj.style = "width:100%;margin:5px;";
			textObj.innerHTML = "<span>" + data.l + "</span><span> : </span><span>" + data.r + "</span>";
			return textObj;
		},
		createNoteData: function(noteData) {
			var newNoteBox = document.createElement("div");
			newNoteBox.style =
				"width: 60%;margin:20%;margin-top: 20px; text-align: center; line-height: 23px;";
			var newNoteText;
			if (Array.isArray(noteData)) {
				for (let i = 0; i < noteData.length; i++) {
					newNoteText = this.createNoteText(noteData[i]);
					newNoteBox.appendChild(newNoteText);
				}
			} else {
				newNoteText = this.createNoteText(noteData);
				newNoteBox.appendChild(newNoteText);
			}
			return newNoteBox;
		},
		//createNote方法创建一个文字性的提示对象
		createNote: function(tittle, noteData) {
			var ResObj = false;
			if (tittle && noteData) {
				ResObj = document.createElement("div");
				var newTittle = this.createTittle(tittle);
				var newNote = this.createNoteData(noteData);
				ResObj.appendChild(newTittle);
				ResObj.appendChild(newNote);
			}
			return ResObj;
		},
		//createPage方法创建一个增强设置页
		createPage: function(val) {
			var settingBox = document.createElement("div");
			settingBox.id = "console-moreSet-content";
			settingBox.style =
				"width: 100%;height: 100px;flex-grow: 1;overflow: auto;box-sizing: border-box;padding: 0 25px 0 0;margin: 10px 0;display: none";
			var newOpt;
			for (let i = 0; i < val.length; i++) {
				if (val[i].type) {
					if (val[i].type === "note") {
						newOpt = this.createNote(val[i].tittle, val[i].noteData);
						settingBox.appendChild(newOpt);
					}
				} else {
					newOpt = this.createOpt(val[i].tittle, val[i].value, val[i].choice);
					settingBox.appendChild(newOpt);
				}
			}
			document.getElementById("console-box").appendChild(settingBox);
		},
		//createMenu方法在设置菜单中创建增强设置功能入口
		createMenu: function() {
			var menuBtn = document.createElement("div");
			menuBtn.setAttribute("class", "console-menu-btn");
			menuBtn.id = "moreSetBtn";
			menuBtn.innerText = "增强设置";
			menuBtn.onclick = this.on;
			document.getElementById("console-menu-main").appendChild(menuBtn);
		},
		//createSaveBtn方法创建一个保存按钮对象
		createSaveBtn: function() {
			var oldSaveBox = document.getElementById("save-box");
			var newSaveBox = document.createElement("div");
			newSaveBox.style.display = "none";
			newSaveBox.id = "new-save-box";
			var newSaveBtn = document.createElement("div");
			newSaveBtn.style =
				"font-size: 14px;display: flex;justify-content: center;align-items: center;background-color: #4486f6;color: white;height: 25px;width: 120px;border-radius: 100px;margin-right: 10px;cursor: pointer;";
			var newSaveIcon = document.createElement("img");
			newSaveIcon.style = "width: 13px;height: 13px;margin-right: 5px;";
			newSaveIcon.setAttribute("src", "/static/home/images/console/saveicon.svg");
			var newSaveTittle = document.createElement("span");
			newSaveTittle.innerText = "保存并应用";
			newSaveBtn.onclick = function() {
				if (saveFlag) {
					store.set("settingsData", settings.getAll());
					saveFlag = false;
					console.log("保存增强设置");
					setTimeout(function() {
						var msgBox = document.getElementById("msg-box");
						msgBox.style =
							"opacity:0.8;margin-top:50px;display:inline-block;";
						msgBox.innerText = "增强设置保存成功";
						setTimeout(function() {
							location.reload();
						}, 500);
					}, 300);
				}
				document.getElementById("console-close-btn").click();
			};
			newSaveBtn.appendChild(newSaveIcon);
			newSaveBtn.appendChild(newSaveTittle);
			newSaveBox.appendChild(newSaveBtn);
			oldSaveBox.parentElement.insertBefore(newSaveBox, oldSaveBox);
		},
		//on方法是增强设置页面的启动方法
		on: function() {
			var moreSetPage = document.getElementById("console-moreSet-content");
			if (moreSetPage) {
				document.getElementsByClassName("console-title-img")[0].src =
					"/static/home/images/console/set1.svg";
				document.getElementsByClassName("console-title")[0].innerText = "增强设置";
				document.getElementById("console-menu").style.display = "none";
				moreSetPage.style.display = "block";
			} else {
				console.log("增强设置页不存在");
			}
		},
		//off方法是增强设置页面的关闭/隐藏方法
		off: function() {
			var moreSetPage = document.getElementById("console-moreSet-content");
			var newSaveBox = document.getElementById("new-save-box");
			if (moreSetPage && moreSetPage.style.display !== "none") {
				moreSetPage.style.display = "none";
			}
			if (newSaveBox.style.display !== "none") {
				newSaveBox.style.display = "none";
			}
		},
		//initPC方法是针对PC模式的增强设置初始化方法,是增强设置的启动方法
		initPC: function() {
			var consoleBox = document.getElementById("console-box");
			if (consoleBox) {
				var closeBtn = document.getElementById("console-close-btn");
				if (closeBtn) {
					closeBtn.addEventListener("click", newSettingsPageFn.off);
				}
				this.createMenu();
				this.createSaveBtn();
				this.createPage(this.optData);
			}
		},
		//initMobile方法是针对Mobile模式的增强设置初始化方法,是增强设置的启动方法
		initMobile: function() {
			var menuWrap = document.getElementById("menu-wrap");
			if (menuWrap) {
				var menuObj = menuWrap.children[1];
				if (menuObj && menuObj.tagName === "UL") {
					var newOpt = document.createElement("li");
					newOpt.innerHTML = "<img src='/static/home/images/console/set1.svg'/><span>增强设置</span>";
					newOpt.onclick = function() {
						var res = confirm("增强设置请前往电脑版操作,点击确认前往电脑版");
						if (res) {
							location.href = "https://www.jianfast.com/?pc=1";
						}
					}
					menuObj.appendChild(newOpt);
				}
				var menuNotice = document.getElementById("menu-notice");
				var newNotice = "<div>" + menuNotice.children[1].innerText +
					"</div><div>对增强设置进行修改请前往电脑版网页操作</div><div>增强设置-搜索结果打开方式仅对电脑版生效</div>";
				menuNotice.children[1].innerHTML = newNotice;
			}
		},
		//init方法调用initPC方法与initMobile方法初始化增强设置,是对外统一调用的初始化方法
		init: function() {
			this.initPC();
			this.initMobile();
		}
	};
	//saveFlag为全局变量,用于判断是否需要保存增强设置
	var saveFlag = false;
	//启动主程序
	settings.on();
})();