frisch's Custom Search Context Entries

Custom Search Engines added to the custom context menu. Requires my Userscript Extender https://greasyforks.org/en/scripts/24896-frisch-s-userscript-extender

Version vom 16.11.2016. Aktuellste Version

// ==UserScript==
// @name         frisch's Custom Search Context Entries
// @namespace    http://null.frisch-live.de/
// @version      0.05
// @description  Custom Search Engines added to the custom context menu. Requires my Userscript Extender https://greasyforks.org/en/scripts/24896-frisch-s-userscript-extender
// @author       frisch
// @grant        GM_openInTab
// @include        *
// ==/UserScript==
console.log("Initializing frisch's Custom Search Context Entries...");
var jq = document.fExt.jq;

// Add your own search engines here
// Use SEARCHSTRING as the replacer for the actual search term (the selected text)
var searchEngines = [
    {
        category: "General",
        name: "Google",
        url: "https://www.google.com/search?q=SEARCHSTRING&oq=SEARCHSTRING",
        id: "csGoogle"
    },
    {
        category: "Images",
        name: "Google Images",
        url: "https://www.google.com/search?tbm=isch&q=SEARCHSTRING",
        id: "csImageByName"
    },
    {
        category: "Videos",
        name: "YouTube",
        url: "https://www.youtube.com/results?search_query=SEARCHSTRING",
        id: "csYouTube"
    },
];

var selectedText;

var sub = document.fExt.ctxMenu.addSub("Search Engines");

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;
});

for(i = 0; i < searchEngines.length; i++) {
    var engine = searchEngines[i];
    var ctxItem = document.fExt.ctxMenu.addItem(engine.category + " - " + engine.name, engine.id, sub);
    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;
    };
}

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);

    console.log("Opening: '" + url + "'");
    GM_openInTab(url, true);
}

jq("#fExtContextMenu").on("fExtContextMenuOpening", function(event, actor){
    selectedText = document.fExt.getSelection() || document.fExt.getSource(actor.get(0));

    jq(searchEngines).each(function(){ this.ctxItem.Toggle(selectedText.length > 0); });
});
长期地址
遇到问题?请前往 GitHub 提 Issues。