Skip to content

Instantly share code, notes, and snippets.

@metasong
Last active October 11, 2023 09:39
Show Gist options
  • Save metasong/96a522a6d76b6625a48e8392532a9180 to your computer and use it in GitHub Desktop.
Save metasong/96a522a6d76b6625a48e8392532a9180 to your computer and use it in GitHub Desktop.
Surfingkeys
/// https://gist.githubusercontent.com/metasong/96a522a6d76b6625a48e8392532a9180/raw/option.js
// config for:
// https://github.com/brookhong/Surfingkeys
// default mappings:
// https://github.com/brookhong/Surfingkeys/blob/master/src/content_scripts/common/default.js
// Note:
// command: [number of repeat or position(index or time)][action][object]
// Microsoft Edge Shortcuts:
// https://support.microsoft.com/en-us/microsoft-edge/keyboard-shortcuts-in-microsoft-edge-50d3edab-30d9-c7e4-21ce-37fe2713cfad
// debug: use console.log("test....")
settings.hintAlign = 'left';
settings.showTabIndices = true;
api.unmap('u'); // u is used for undo
api.unmap("'"); // ' is used for goto
api.unmap('0'); // 0 is used for reset
/*
** Navigation in collection or tree:
** /// index based position
** // 1 dimension collection
** {: fist or root
** }: last or last leaf
** ]: next
** [: previous
** // tree: multiple dimension collection
** >: next in tree: first child of the parent or next sibling if no children
** <: previous in tree: parent or previous sibling if no parent
**
** // time(') based: {': oldest; }': newest
**
** ': go to
** x: close
** `: toggle
** ,: edit - not . means not finished, so editing
** =: set, save
** +: add,new
** -: remove
** _: get, restore - _ download from up(cloud)
** \\: undo
** 0: restore; reset;reload;refresh
** |: move
** o: open
** d: show, display
** s: search
** ;: others
**
** note:
** all command could be prefix with numbers to repeat.
** the prefix number for 'to' command is the object index in collection
*/
keyMaps = [
// f: Open a link, press SHIFT to flip overlapped hints, hold SPACE to hide hints
// t: open url
// b: bookmark
// H: history
// :: command
['<F1>', '?', false],
[']t', 'R', true],// switch to the tab right
['[t', 'E', true],// switch to the tab left
['{t', 'g0', true],
['}t', 'g$', true],
[']s', 'cs', true], //
[']h', 'D', false], //3
['[h', 'S', false],
["['t", '<Ctrl-6>', true], // last used tab
["{'", 'gT', true], // first actived tab
["}'", 'gt', true], // last actived tab
// active tab history
["['", 'B'], // one tab history back;
["]'", 'F'], // one tab history forward;
['[u', 'gu', true], // go up one path in url
['{u', 'gU', true], // goto root url
["'p",'gp',true], // goto play tab
// ["'p",'gp',true], // goto play tab
['<Alt-t>', '<Alt-s>', true], // toggle surfkey
['<Alt-s>', 'd', true], // scroll half page down
['<Alt-w>', 'e', true], // scroll half page up
['{i', 'gi', true], // first input
// display hints for...
['dh', '<Ctrl-h>', true], // display hover
['dH', '<Ctrl-j>', true], // undisplay hover
['ds', ';fs', true], // focus scrollable elements
['di', 'q', true], // click image or button
['dvs', 'zv', true], // visual element select
['dq', 'cq', true],// query word to search in dictionary
// add
['+t','on', true],// on: new tab; ctrl-t; ctrl-n: new window
['+z', 'zi', true], // zoom in; ctrl-=
// remove
['-h', ';dh', true], // delete history older than 30 days
['-b', ';db', true], // remove bookmark of this page
['-z', 'zo', true], // zoom out; ctrl--
//reload, refresh, reset
['00', 'r', true], // ctrl-r and F5
['0s', 'cS', true], // refresh scrollables
['0#', 'g#', true], // reload without hash
['0?', 'g?', true], // reload without query string
['0z', 'zr', true, '#5reset zoom'], //reset zoom
// search and show
// ['sb', 'ob', true],
// ['sb', 'oh', true],
// ['sg', 'og', true],
// ['sd', 'od', true],
// ['sw', 'ow', true],
// ['sy', 'oy', true],
// open
// ['oh', 'H', false], // open one tab history in new tab
// ['os', 'se', true, '#11Open settings'],
// ['oba', 'ga', true], // browser about
// ['obb', 'gb', true], // browser bookmark
// ['obc', 'gc', true], // browser cache
// ['obd', 'gd', true], // browser download
// ['obh', 'gh', true], // browser history
// ['obk', 'gk', true], // browser cookies
// ['obe', 'ge', true], // browser extensions
// ['obn', 'gn', true], // browser net-internals
// ['obs', 'gs', true], // browser page source
// ['obi', 'si', true], // browser inspect
['om', 'sm', true], // markdown preview
// f: fast open a url, press SHIFT to flip overlapped hints, hold SPACE to hide hints
// t: open a url in new tab
['oo', 'go', true], // open url in current tab
['oumt', 'cf', true], // open multiple links in non-active tab
['out', 'gf', true], // open the link in non-active tab
['ouT', 'af', true], // show hint for links and open it in new tab
['oux', 'ox',true], // open recently closed url
// close
// X: restore closed tab(ctrl-shift-t)
// ['xx', 'x', true], // close current tab (ctrl-w)
['xd', ';j', true], // close download shelf
['x}', 'gx$', true],
['x{', 'gx0', true],
['x[', 'gxt', true],
['x]', 'gxT', true],
["x'", 'gxx', true], // close all except current
['xp', 'gxp', true], // close playing tab
// move
['|[', '<<', true],
['|]', '>>', true],
['|o', 'W', true],
// copy(yank)
['ypf', 'yp', true], // yank form data for post
['ypi', ';cp', true], // yank proxy info
// past
['pp', ';ap', true], // apply proxy info from clipboard
['pf', ';pf', true], // fill form with data from yf
// edit
[",ut", ';u', true, '#5edit url with vim and open in new tab'],
[",ul", ';U', true, '#5edit url and load'],
[',o', ';e', true],
// undo, unset, clear
['\\h', ';m', true], // unhover
['\\c', 'X'], // undo tab close ctrl-shift-t
// set, save,
['=pa', ';pa', true], // set proxy mode 'always'
['=pb', ';pb', true], // set proxy mode 'byhost'
['=pd', ';pd', true], // set proxy mode 'direct'
['=ps', ';ps', true], // set proxy mode 'system'
['=pc', ';pc', true], // set proxy mode 'clear'
['=s', 'ZZ', true, '#5Save session and quit'],
// get, restore
['_i', ';di', true], // download image
['_s', 'ZR', true, '#5Restore last saved session'],
['_l', ';ql', true], // show last action
// toggle
['`pi', '<Alt-p>', true], // pin
['`pr', 'cp', true], // proxy
['`m', '<Alt-m>', true], // mute ctrl-m
// mis
['gi', ';i', true], // open chrome inspect
];
function rmap(newKey, oldKey, ummapOldKey, domain, annotation) { // replacing map
api.map(newKey, oldKey, domain, annotation);
!!ummapOldKey && api.unmap(oldKey);
}
// ['x', 'xx', true] notworking, so it's a bug, workaround:
api.unmap('x');// close current tab
api.mapkey('xx', '#1Close current', function(){api.RUNTIME("closeTab")});
keyMaps.forEach(map => {
rmap(map[0], map[1], map[2], undefined, map[3]);
});
api.removeSearchAlias('g');// remove default search alias for google
api.removeSearchAlias('d');// remove default search alias for duckduckgo
api.removeSearchAlias('b');// remove default search alias for baidu
api.removeSearchAlias('e');// remove default search alias for wikipedia
api.removeSearchAlias('w');// remove default search alias for bing
api.removeSearchAlias('s');// remove default search alias for stackoverflow
api.removeSearchAlias('h');// remove default search alias for github
api.removeSearchAlias('y');// remove default search alias for youtube
api.addSearchAlias('sg', 'google', 'https://www.google.com/search?q=', 's', 'https://www.google.com/complete/search?client=chrome-omni&gs_ri=chrome-ext&oit=1&cp=1&pgcl=7&q=', function(response) {
var res = JSON.parse(response.text);
return res[1];
});
api.addSearchAlias('sd', 'duckduckgo', 'https://duckduckgo.com/?q=', 's', 'https://duckduckgo.com/ac/?q=', function(response) {
var res = JSON.parse(response.text);
return res.map(function(r){
return r.phrase;
});
});
api.addSearchAlias('sb', 'baidu', 'https://www.baidu.com/s?wd=', 's', 'https://suggestion.baidu.com/su?cb=&wd=', function(response) {
var res = response.text.match(/,s:\[("[^\]]+")]}/);
return res ? res[1].replace(/"/g, '').split(",") : [];
});
api.addSearchAlias('se', 'wikipedia', 'https://en.wikipedia.org/wiki/', 's', 'https://en.wikipedia.org/w/api.php?action=opensearch&format=json&formatversion=2&namespace=0&limit=40&search=', function(response) {
return JSON.parse(response.text)[1];
});
api.addSearchAlias('sw', 'bing', 'https://www.bing.com/search?setmkt=en-us&setlang=en-us&q=', 's', 'https://api.bing.com/osjson.aspx?query=', function(response) {
var res = JSON.parse(response.text);
return res[1];
});
api.addSearchAlias('ss', 'stackoverflow', 'https://stackoverflow.com/search?q=');
// the default 'h' is used in 'oh' command for history.
api.addSearchAlias('su', 'github', 'https://github.com/search?q=', 's', 'https://api.github.com/search/repositories?order=desc&q=', function(response) {
var res = JSON.parse(response.text)['items'];
return res ? res.map(function(r){
return {
title: r.description,
url: r.html_url
};
}) : [];
});
api.addSearchAlias('sy', 'youtube', 'https://www.youtube.com/results?search_query=', 's',
'https://clients1.google.com/complete/search?client=youtube&ds=yt&callback=cb&q=', function(response) {
var res = JSON.parse(response.text.substr(9, response.text.length-10));
return res[1].map(function(d) {
return d[0];
});
});
api.Front.registerInlineQuery({
url: function(q) {
return `http://dict.youdao.com/w/eng/${q}/#keyfrom=dict2.index`;
},
parseResult: function(res) {
var parser = new DOMParser();
var doc = parser.parseFromString(res.text, "text/html");
var collinsResult = doc.querySelector("#collinsResult");
var authTransToggle = doc.querySelector("#authTransToggle");
var examplesToggle = doc.querySelector("#examplesToggle");
if (collinsResult) {
collinsResult.querySelectorAll("div>span.collinsOrder").forEach(function(span) {
span.nextElementSibling.prepend(span);
});
collinsResult.querySelectorAll("div.examples").forEach(function(div) {
div.innerHTML = div.innerHTML.replace(/<p/gi, "<span").replace(/<\/p>/gi, "</span>");
});
var exp = collinsResult.innerHTML;
return exp;
} else if (authTransToggle) {
authTransToggle.querySelector("div.via.ar").remove();
return authTransToggle.innerHTML;
} else if (examplesToggle) {
return examplesToggle.innerHTML;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment