SelectAndGo

Search with selected text by pressing s(Google), t(Translate) or d(Oxford) within 2 seconds.

Version vom 20.12.2020. Aktuellste Version

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         SelectAndGo
// @namespace    com.gmail.fujifruity.greasemonkey
// @version      1.1
// @description  Search with selected text by pressing s(Google), t(Translate) or d(Oxford) within 2 seconds.
// @author       fujifruity
// @match        *://*/*
// @grant        GM.openInTab
// ==/UserScript==

{
    const timeoutMs = 2000
    const log = (...msg) => console.log('SelectAndGo:', ...msg)

    function onKeydown(event) {
        if (event.target.tagName == "INPUT" || event.ctrlKey || event.altKey) return
        const selection = window.getSelection().toString()
        switch (event.key) {
            case 's':
                GM.openInTab("https://www.google.com/search?q=" + selection, false)
                break
            case 't':
                GM.openInTab("https://translate.google.com/#en/ja/" + selection, false)
                break
            case 'i':
                GM.openInTab("https://www.google.com/search?tbm=isch&q=" + selection, false)
                break
            case 'o': {
                let url = "https://www.oxfordlearnersdictionaries.com/search/english/?q="
                // the website requires hyphen-separated words
                let query = selection.replace(/\s+/g, '-')
                GM.openInTab(url + query, false)
                break
            }
            case 'c':
                alert(window.getSelection().toString().length + ' characters')
                break
        }
    }

    function unsetShortcut() {
        window.removeEventListener('keydown', onKeydown)
    }

    log('listen selectstart')
    window.addEventListener('selectstart', () => {
        window.addEventListener('keydown', onKeydown)
        // unset shortcuts in seconds.
        window.onmouseup = () => {
            setTimeout(unsetShortcut, timeoutMs)
            window.onmouseup = null
        }
        // unset shortcuts when selection is triggerd by ctrl+a
        window.onkeyup = () => {
            setTimeout(unsetShortcut, timeoutMs)
            window.onkeyup = null
        }
    })

}