// ==UserScript== // @name 谷歌搜索 - 优化 // @namespace http://tampermonkey.net/ // @version 0.1.8 // @description 1.屏蔽推广 2.普通样式 3.单列居中 4双列居中 // @author 浮生未歇 // @run-at document-start // @include https://www.google.com*/search* // @exclude https://www.google.com*/search*&source=lnms* // @resource googleCommon https://cdn.jsdelivr.net/gh/sinlin/google@1.2.1/2019-03-16/google-common.css // @resource googleOne https://cdn.jsdelivr.net/gh/sinlin/google@1.2.1/2019-03-16/google-one.css // @resource googleTwo https://cdn.jsdelivr.net/gh/sinlin/google@1.2.1/2019-03-16/google-two.css // @grant GM_addStyle // @grant GM_getResourceText // @grant GM_getResourceURL // @grant GM_setValue // @grant GM_getValue // @grant GM_deleteValue // @grant GM_xmlhttpRequest // @downloadURL https://update.greasyfork.icu/scripts/381472/%E8%B0%B7%E6%AD%8C%E6%90%9C%E7%B4%A2%20-%20%E4%BC%98%E5%8C%96.user.js // @updateURL https://update.greasyfork.icu/scripts/381472/%E8%B0%B7%E6%AD%8C%E6%90%9C%E7%B4%A2%20-%20%E4%BC%98%E5%8C%96.meta.js // ==/UserScript== (() => { //初始化配置 let Configs = { //是否调试 IS_DEBUG: false, //功能配置 OPTIONS: [ //页面布局:1:普通页面,2:单列居中,3:双列居中, { name: "SELECT_PAGE", value: 1 } ], //谷歌样式 GOOGLE_STYLES: { COMMON: GM_getResourceText("googleCommon"), ONE: GM_getResourceText("googleOne"), TWO: GM_getResourceText("googleTwo"), } }; //Google对象参数配置 let googleParamConfig = { Configs, window, document, location }; //Google对象 let Google = (({ Config, window, document, location }, undefined) => { //创建空对象 let Google = Object.create(null); //绑定 Google.ready = functon(){} 功能 Reflect.defineProperty(Google, "ready", { set: fn => { if (document.readyState === "complete") { fn(); } else if (!!document.addEventListener) { document.addEventListener( "DOMContentLoaded", () => { fn(); }, true ); } else { throw new Error("Google.ready can't use"); } } }); /** * 函数执行器 - 组合执行 */ class Command { constructor() { this.commondList = []; } /** * 添加数组缓存中 * @param {object} command 对象实例 */ add(command) { this.commondList.push(command); } /** * 执行数值缓存中的实例 */ execute() { for (let i = 0, command; (command = this.commondList[i++]); ) { command.execute(); } } } /** * GM * 用于调用 TamperMonkey 内置API */ class GM { /** * 获取数据储存 */ getGmValue(selection) { return GM_getValue(selection.name, selection.value); } /** * 设置数据储存 */ SetGmValue(name, value) { GM_setValue(name, value); } /** * 使用GM导入样式 * @param {string} styles 样式 */ importGmStyle(styles) { if (typeof styles === "string") { GM_addStyle(styles); } } } /** * 数据类 * 用于获取跟初始化配置相关的信息 */ class Data extends GM { constructor() { super(); this.DEFAULT_PAGE_LAYOUT_TYPE = Configs.OPTIONS[0]; this.STYLE_CONTENT_COMMON = Configs.GOOGLE_STYLES.COMMON; this.STYLE_CONTENT_ONE_CENTER = Configs.GOOGLE_STYLES.ONE; this.STYLE_CONTENT_TWO_CENTER = Configs.GOOGLE_STYLES.TWO; } getPageLayoutName () { return this.DEFAULT_PAGE_LAYOUT_TYPE["name"]; } getCurrentPageLayoutType() { return Number(this.getGmValue(this.DEFAULT_PAGE_LAYOUT_TYPE)) || 1; } getCommonPageStyleContent() { return this.STYLE_CONTENT_COMMON || ""; } getOneCenterStyleContent() { return this.STYLE_CONTENT_ONE_CENTER || ""; } getTwoCenterStyleContent() { return this.STYLE_CONTENT_TWO_CENTER || ""; } } /** * 基础类 */ class Base extends Data{ isHideElement(element) { return element.style.display === "none"; } hideElement(element) { element.style.display = "none"; } showElement(element) { element.style.display = "block"; } } /** * 样式类 * @class Style */ class Style extends Base { constructor() { super(); this.cache = ""; } /** * 清除缓存 */ clearCache() { this.cache = ""; } /** * 导入样式 */ importStyle() { super.importGmStyle(this.cache); } /** * 将样式内容添加到缓存 * @param {string} styleContent */ add(styleContent) { if (typeof styleContent === "string") { this.cache += styleContent; } else { this.cache += ""; } return this; } /** * 结束标识符 * 开始调用导入功能 * 清空缓存 */ end() { this.importStyle(); this.clearCache(); } } /** * 样式-普通页 */ class StyleByCommon extends Style { constructor() { super(); } /** * 获取普通样式 */ getCommonPageStyleContent() { return super.getCommonPageStyleContent(); } getMulPageStyleContent(){ let currentLayoutType = this.getCurrentPageLayoutType(); let style = ""; switch (currentLayoutType) { //普通样式 case 1: style = ""; break; //单页居中 case 2: style = this.getOneCenterStyleContent(); break; //双页居中 case 3: style = this.getTwoCenterStyleContent(); break; } return style; } init () { super.add(this.getCommonPageStyleContent()) .add(this.getMulPageStyleContent()) .end(); } execute () { this.init(); } } /** * 菜单 */ class MenuNode extends Base { constructor() { super(); this.MENU_BTN_ID = "myGoogleMenuButton"; this.MENU_LISTS_ID = "myGoogleLists"; this.MENU_SAVE_NAME="myGooglesave"; } /** * 获取父节点 */ getContainerNode() { // return document // .getElementById("gb") // .querySelector(".gb_Jf"); //return document.getElementById("ab_options"); return document.getElementById("abar_button_opt"); } getMenuSaveIdName () { return this.MENU_SAVE_NAME; } getPageLayoutName () { return super.getPageLayoutName(); } /** * 获得 HTML - 页面布局选项 * @param content 显示的内容 * @param layoutType 页面布局类型 */ getContentPageSelect(content, layoutType) { let checked = super.getCurrentPageLayoutType() === layoutType ? "checked" : ""; return `