frisch's Custom Search Context Entries

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

当前为 2016-11-16 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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://greasyfork.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); });
});