【问题标题】:Keyboard Shortcuts for google search results page using greasemonkey/tampermonkey? [closed]使用greasemonkey/tampermonkey的谷歌搜索结果页面的键盘快捷键? [关闭]
【发布时间】:2018-01-16 12:02:20
【问题描述】:

请分享您可能想出的任何 js 代码,以便我可以重复使用它。谢谢

【问题讨论】:

  • 您有疑问/遇到问题,还是只是想分享您的代码?如果是前者,请更新您的帖子,详细说明您遇到的问题/错误,以及您为尝试修复它所做的工作。如果是后者,Stack Overflow 不是合适的地方——而是查看 GitHub 或类似的地方。

标签: javascript search tampermonkey shortcuts


【解决方案1】:

下面是一些谷歌搜索结果页面快捷方式的工作代码。请随意即兴创作,谢谢

shift + I:将打开图像, shift + V:将打开视频, ... ... ...

您将在搜索结果超链接(链接)旁边看到链接编号 如果您按键盘上的数字,它将在新标签中打开该链接。

你需要greasemonkey或者tampermonkey来运行这个js代码。

我利用其中一位开发人员在Way to Create Keyboard Shortcuts for Google Search Results? 中写的内容对其进行了一些改进

希望这会有所帮助。

谢谢,

阿比。

// ==UserScript==
// @name         Google digits
// @include      https://www.google.tld/*
// @run-at       document-start
// @author       Abhinay Gadikoppula
// @match        https://stackoverflow.com/questions/39456608/way-to-create-keyboard-shortcuts-for-google-search-results
// @grant        none
// ==/UserScript==
// only work on search pages with #q= &q= ?q=
if (location.href.match(/[#&?]q=/)) {

    var links = [];
    var menu_links = [];
    var menu_links_map = [];

    window.onload = function() {

        // get all header menu links
        menu_links = document.getElementById("hdtb-msb").querySelectorAll('a.q.qs');
        menu_links.forEach(function(link, index) {
            menu_links_map[link.innerHTML.charAt(0)] = link;
        });

        // get all results in an array
        links = document.querySelectorAll('h3.r a');
        links.forEach(function(link, index) {
            link.innerHTML = link.innerHTML + ' (' + (index + 1) + ')';
        });
    };

    window.addEventListener('keydown', function(e) {

        if (e.shiftKey && e.keyCode >= 65 && e.keyCode <= 90) {
            e.preventDefault();
            char = String.fromCharCode(e.keyCode);
            //keycodes 65-90 are alphabets a-z
            if (char in menu_links_map) {
                menu_links_map[char].click();
            }
        } else if (e.keyCode >= 49 && e.keyCode <= 57 &&
            // don't intercept if a modifier key is held
            !e.altKey && !e.ctrlKey && !e.shiftKey && !e.metaKey &&
            // don't intercept 1-9 in the search input
            e.target.localName != 'input') {
            //keycodes for 1-9 are 49-57 but replacing them with 1-9
            var digit = e.keyCode - 48;
            // arrays are 0-based
            var link = links[digit - 1];
            if (link) {
                // go to the linked URL
                window.open(link.href, '_blank');
                // prevent site from seeing this keyboard event
                e.preventDefault();
                e.stopPropagation();
                e.stopImmediatePropagation();
            }
        } else if (e.keyCode == 191) {
            // event '/' keycode 191
            e.preventDefault();
            input = document.getElementById("lst-ib");
            val = input.value; //store the value of the element
            input.value = ''; //clear the value of the element
            input.focus(); //set focus on the input field
            input.value = val; //set that value back.
        }
    }, true); // true means we capture the event before it's "bubbled" down
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 2017-01-20
    • 2013-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多