// ==UserScript== // @name frisch's Custom Search // @namespace http://null.frisch-live.de/ // @version 0.09 // @description Custom Search Engines added to the custom context menu. Requires my Userscript Extender https://greasyfork.org/en/scripts/24896-frisch-s-userscript-extender // @author frisch // @grant GM_openInTab // @grant GM_getValue // @grant GM_setValue // @include * // @downloadURL none // ==/UserScript== console.log("Initializing frisch's Custom Search Context Entries..."); var jq = document.fExt.jq; // Variables var searchEngines = [ { category: "General", name: "Google", url: "https://www.google.com/search?q=SEARCHSTRING&oq=SEARCHSTRING", enabled: true }, ]; var selectedText; var searchSub = document.fExt.ctxMenu.addCtxSub("Search by Engine"); var entryHeight = 32; // Styles document.fExt.createStyle("#fcsEngines { position: fixed; width: 900px; border: 1px solid black; display: block; padding: 12px; z-index: 1000; background-color: #FEFEFE; top: 0; bottom: 0; right: 0; left: 0; margin: auto; }"); document.fExt.createStyle("#fcsEngines * { z-index: 1000; margin: 4px; }"); document.fExt.createStyle("#fcsEngines input, #fcsEngines input, #fcsEngines select { height: 20px !important; }"); document.fExt.createStyle("#fcsEngines input[type=button] { width: 100px !important; }"); document.fExt.createStyle("#fcsEngines a { padding: 4px; border: 1px solid grey; float: right; }"); document.fExt.createStyle("#fcsEngines #fcsAddEngine { width: 100px; }"); document.fExt.createStyle("#fcsEngines td * { width: 100%; float: left; }"); document.fExt.createStyle("#fcsEngines td { padding: 0 4px; }"); // Initialization function Initialize(){ LoadEngines(); SortEngines(); SetCtxEntries(); } // Functions function SetCtxEntries(){ searchSub.Clear(); for(i = 0; i < searchEngines.length; i++) { var engine = searchEngines[i]; if(engine.enabled) { var ctxItem = document.fExt.ctxMenu.addCtxItem(engine.category + " - " + engine.name, searchSub); engine.ctxItem = ctxItem; engine.ctxItem.Engine = engine; engine.ctxItem.Action = function(event, sender, actor) { var searchStrings = selectedText.split("\n"); for(i = 0; i < searchStrings.length; i++) { if(searchStrings[i] !== "") Search(this.Engine, searchStrings[i]); } return false; }; } } document.fExt.ctxMenu.addSeparator(searchSub); var settingsItem = document.fExt.ctxMenu.addCtxItem("Settings", searchSub); settingsItem.Action = function(){ ShowEngines(); }; } function Search(engine, text){ var url = engine.url; var sText = text.replace(" ", "+"); // sText = escape(sText); while(url.indexOf("SEARCHSTRING") >= 0) url = url.replace("SEARCHSTRING", sText); url = encodeURI(url); console.log("Opening: '" + url + "'"); GM_openInTab(url, true); } function SortEngines(){ searchEngines.sort(function(a, b){ if(a.category > b.category) return 1; else if(a.category < b.category) return -1; if(a.name > b.name) return 1; else if(a.name < b.name) return -1; return 0; }); } function LoadEngines(){ var lEngines = GM_getValue("fcsSearchEngines"); if(lEngines !== undefined){ searchEngines = JSON.parse(lEngines.toString()); for(var i = 0; i < searchEngines.length; i++){ if(searchEngines[i].enabled === undefined) searchEngines[i].enabled = true; } document.fExt.popup("Search Engines loaded."); } } function SaveEngines() { var saveEngines = searchEngines.splice(0); for(var i = 0; i < saveEngines.length; i++) saveEngines[i].ctxItem = undefined; GM_setValue("fcsSearchEngines", JSON.stringify(saveEngines)); document.fExt.popup("Search Engines saved."); } function ShowEngines(){ var height = 100; var settingsDiv = jq("
Name | Category | Url | Enabled | |
" + " | " + CreateCategoryDropDown(engine.category) + " | " + "" + " | " + " | " + " |
" + " | " + CreateCategoryDropDown() + " | " + "" + " | " + " |