Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
my keysnail set up
// ========================== KeySnail Init File =========================== //
// You can preserve your code in this area when generating the init file using GUI.
// Put all your code except special key, set*key, hook, blacklist.
// ========================================================================= //
//{{%PRESERVE%
// Put your codes here
//}}%PRESERVE%
// ========================================================================= //
// ========================= Special key settings ========================== //
key.quitKey = "C-g";
key.helpKey = "<f1>";
key.escapeKey = "C-q";
key.macroStartKey = "<f3>";
key.macroEndKey = "<f4>";
key.suspendKey = "<f2>";
key.universalArgumentKey = "C-u";
key.negativeArgument1Key = "C--";
key.negativeArgument2Key = "C-M--";
key.negativeArgument3Key = "M--";
// ================================= Hooks ================================= //
hook.addToHook('KeyBoardQuit', function (aEvent) {
if (key.currentKeySequence.length)
return;
command.closeFindBar();
var marked = command.marked(aEvent);
if (util.isCaretEnabled())
{
if (marked)
{
command.resetMark(aEvent);
}
else
{
if ("blur" in aEvent.target) aEvent.target.blur();
gBrowser.focus();
_content.focus();
}
}
else
{
goDoCommand("cmd_selectNone");
}
if (KeySnail.windowType === "navigator:browser" && !marked)
{
key.generateKey(aEvent.originalTarget, KeyEvent.DOM_VK_ESCAPE, true);
}
});
// ============================= Key bindings ============================== //
key.setGlobalKey('C-M-r', function (ev) {
userscript.reload();
}, 'Reload the initialization file', true);
key.setGlobalKey('M-x', function (ev, arg) {
ext.select(arg, ev);
}, 'List exts and execute selected one', true);
key.setGlobalKey('M-:', function (ev) {
command.interpreter();
}, 'Command interpreter', true);
key.setViewKey([",", "h", "b"], function (ev) {
key.listKeyBindings();
}, 'List all keybindings', false);
key.setGlobalKey(["<f1>", "b"], function (ev) {
key.listKeyBindings();
}, 'List all keybindings', false);
key.setGlobalKey('C-m', function (ev) {
key.generateKey(ev.originalTarget, KeyEvent.DOM_VK_RETURN, true);
}, 'Generate the return key code', false);
key.setGlobalKey(["<f1>", "F"], function (ev) {
openHelpLink("firefox-help");
}, 'Display Firefox help', false);
key.setGlobalKey(["C-x", "l"], function (ev) {
command.focusToById("urlbar");
}, 'Focus to the location bar', true);
key.setGlobalKey(["C-x", "g"], function (ev) {
command.focusToById("searchbar");
}, 'Focus to the search bar', true);
key.setGlobalKey(["C-x", "t"], function (ev) {
command.focusElement(command.elementsRetrieverTextarea, 0);
}, 'Focus to the first textarea', true);
key.setGlobalKey(["C-x", "s"], function (ev) {
command.focusElement(command.elementsRetrieverButton, 0);
}, 'Focus to the first button', true);
key.setGlobalKey('M-w', function (ev) {
command.copyRegion(ev);
}, 'Copy selected text', true);
key.setGlobalKey('C-s', function (ev) {
command.iSearchForwardKs(ev);
}, 'Emacs like incremental search forward', true);
key.setGlobalKey('C-r', function (ev) {
command.iSearchBackwardKs(ev);
}, 'Emacs like incremental search backward', true);
key.setViewKey([",", "x", "k"], function (ev) {
BrowserCloseTabOrWindow();
}, 'Close tab / window', false);
key.setGlobalKey(["C-x", "k"], function (ev) {
BrowserCloseTabOrWindow();
}, 'Close tab / window', false);
key.setViewKey([",", "x", "u"], function (ev) {
undoCloseTab();
}, 'Undo closed tab', false);
key.setGlobalKey(["C-c", "u"], function (ev) {
undoCloseTab();
}, 'Undo closed tab', false);
key.setGlobalKey(["C-x", "n"], function (ev) {
OpenBrowserWindow();
}, 'Open new window', false);
key.setGlobalKey('C-M-l', function (ev) {
getBrowser().mTabContainer.advanceSelectedTab(1, true);
}, 'Select next tab', false);
key.setGlobalKey('C-M-h', function (ev) {
getBrowser().mTabContainer.advanceSelectedTab(-1, true);
}, 'Select previous tab', false);
key.setGlobalKey(["C-x", "C-c"], function (ev) {
goQuitApplication();
}, 'Exit Firefox', true);
key.setGlobalKey(["C-x", "o"], function (ev, arg) {
command.focusOtherFrame(arg);
}, 'Select next frame', false);
key.setEditKey('C-a', function (ev) {
command.beginLine(ev);
}, 'Beginning of the line', false);
key.setEditKey('C-e', function (ev) {
command.endLine(ev);
}, 'End of the line', false);
key.setGlobalKey(["C-x", "1"], function (ev) {
window.loadURI(ev.target.ownerDocument.location.href);
}, 'Show current frame only', true);
key.setGlobalKey(["C-x", "C-f"], function (ev) {
BrowserOpenFileWindow();
}, 'Open the local file', true);
key.setGlobalKey(["C-x", "C-s"], function (ev) {
saveDocument(window.content.document);
}, 'Save current page to the file', true);
key.setGlobalKey(["C-c", "C-c", "C-v"], function (ev) {
toJavaScriptConsole();
}, 'Display JavaScript console', true);
key.setGlobalKey(["C-c", "C-c", "C-c"], function (ev) {
command.clearConsole();
}, 'Clear Javascript console', true);
key.setEditKey(["C-x", "h"], function (ev) {
command.selectAll(ev);
}, 'Select whole text', true);
key.setEditKey([["C-SPC"], ["C-@"]], function (ev) {
command.setMark(ev);
}, 'Set the mark', true);
key.setEditKey('C-o', function (ev) {
command.openLine(ev);
}, 'Open line', false);
key.setEditKey([["C-x", "u"], ["C-_"]], function (ev) {
display.echoStatusBar("Undo!", 2000);
goDoCommand("cmd_undo");
}, 'Undo', false);
key.setEditKey('C-\\', function (ev) {
display.echoStatusBar("Redo!", 2000);
goDoCommand("cmd_redo");
}, 'Redo', false);
key.setEditKey('M-f', function (ev) {
command.forwardWord(ev);
}, 'Next word', false);
key.setEditKey('M-b', function (ev) {
command.backwardWord(ev);
}, 'Previous word', false);
key.setEditKey('C-n', function (ev) {
command.nextLine(ev);
}, 'Next line', false);
key.setEditKey('C-p', function (ev) {
command.previousLine(ev);
}, 'Previous line', false);
key.setEditKey('C-v', function (ev) {
command.pageDown(ev);
}, 'Page down', false);
key.setEditKey('M-v', function (ev) {
command.pageUp(ev);
}, 'Page up', false);
key.setEditKey('M-<', function (ev) {
command.moveTop(ev);
}, 'Beginning of the text area', false);
key.setEditKey('M->', function (ev) {
command.moveBottom(ev);
}, 'End of the text area', false);
key.setEditKey('C-d', function (ev) {
goDoCommand("cmd_deleteCharForward");
}, 'Delete forward char', false);
key.setEditKey('C-h', function (ev) {
goDoCommand("cmd_deleteCharBackward");
}, 'Delete backward char', false);
key.setEditKey('M-d', function (ev) {
command.deleteForwardWord(ev);
}, 'Delete forward word', false);
key.setEditKey([["C-<backspace>"], ["M-<delete>"]], function (ev) {
command.deleteBackwardWord(ev);
}, 'Delete backward word', false);
key.setEditKey('M-u', function (ev, arg) {
command.wordCommand(ev, arg, command.upcaseForwardWord, command.upcaseBackwardWord);
}, 'Convert following word to upper case', false);
key.setEditKey('M-l', function (ev, arg) {
command.wordCommand(ev, arg, command.downcaseForwardWord, command.downcaseBackwardWord);
}, 'Convert following word to lower case', false);
key.setEditKey('M-c', function (ev, arg) {
command.wordCommand(ev, arg, command.capitalizeForwardWord, command.capitalizeBackwardWord);
}, 'Capitalize the following word', false);
key.setEditKey('C-k', function (ev) {
command.killLine(ev);
}, 'Kill the rest of the line', false);
key.setEditKey('C-y', command.yank, 'Paste (Yank)', false);
key.setEditKey('M-y', function (ev) {
if (!command.kill.ring.length)
return;
var ct = command.getClipboardText();
(!command.kill.ring.length || ct != command.kill.ring[0]) && command.pushKillRing(ct);
prompt.selector(
{
message: "Paste:",
collection: command.kill.ring,
callback: function (i) { if (i >= 0) key.insertText(command.kill.ring[i]); }
}
);
}, 'Show kill-ring and select text to paste', true);
key.setEditKey('C-w', function (ev) {
goDoCommand("cmd_copy");
goDoCommand("cmd_delete");
command.resetMark(ev);
}, 'Cut current region', true);
key.setEditKey(["C-x", "r", "d"], function (ev, arg) {
command.replaceRectangle(ev.originalTarget, "", false, !arg);
}, 'Delete text in the region-rectangle', true);
key.setEditKey(["C-x", "r", "t"], function (ev) {
prompt.read("String rectangle: ", function (aStr, aInput) {
command.replaceRectangle(aInput, aStr);
},
ev.originalTarget);
}, 'Replace text in the region-rectangle with user inputted string', true);
key.setEditKey(["C-x", "r", "o"], function (ev) {
command.openRectangle(ev.originalTarget);
}, 'Blank out the region-rectangle, shifting text right', true);
key.setEditKey(["C-x", "r", "k"], function (ev, arg) {
command.kill.buffer = command.killRectangle(ev.originalTarget, !arg);
}, 'Delete the region-rectangle and save it as the last killed one', true);
key.setEditKey(["C-x", "r", "y"], function (ev) {
command.yankRectangle(ev.originalTarget, command.kill.buffer);
}, 'Yank the last killed rectangle with upper left corner at point', true);
key.setEditKey('M-n', function (ev) {
command.walkInputElement(command.elementsRetrieverTextarea, true, true);
}, 'Focus to the next text area', false);
key.setEditKey('M-p', function (ev) {
command.walkInputElement(command.elementsRetrieverTextarea, false, true);
}, 'Focus to the previous text area', false);
key.setViewKey([["C-n"], ["j"]], function (ev) {
key.generateKey(ev.originalTarget, KeyEvent.DOM_VK_DOWN, true);
}, 'Scroll line down', false);
key.setViewKey([["C-f"], ["."]], function (ev) {
key.generateKey(ev.originalTarget, KeyEvent.DOM_VK_RIGHT, true);
}, 'Scroll right', false);
key.setViewKey([["M-v"], ["b"]], function (ev) {
goDoCommand("cmd_scrollPageUp");
}, 'Scroll page up', false);
key.setViewKey('C-v', function (ev) {
goDoCommand("cmd_scrollPageDown");
}, 'Scroll page down', false);
key.setViewKey('l', function (ev) {
getBrowser().mTabContainer.advanceSelectedTab(1, true);
}, 'Select next tab', false);
key.setViewKey('h', function (ev) {
getBrowser().mTabContainer.advanceSelectedTab(-1, true);
}, 'Select previous tab', false);
key.setViewKey(':', function (ev, arg) {
shell.input(null, arg);
}, 'List and execute commands', true);
key.setViewKey('R', function (ev) {
BrowserReload();
}, 'Reload the page', true);
key.setGlobalKey(['C-c','C-r'], function (aEvent, aArg) {
BrowserReload();
}, 'Reload page', true);
key.setViewKey('B', function (ev) {
BrowserBack();
}, 'Back', false);
function myBack3Page (aEvent, aArg){
var nthHistory = 3;
content.history.go(-1 * nthHistory);
}
key.setViewKey('V', myBack3Page, 'Back 3 pages');
key.setGlobalKey(['C-c','C-v'], myBack3Page, 'Back 3 pages');
key.setViewKey('F', function (ev) {
BrowserForward();
}, 'Forward', false);
key.setViewKey(["C-x", "h"], function (ev) {
goDoCommand("cmd_selectAll");
}, 'Select all', true);
key.setViewKey('f', function (ev) {
command.focusElement(command.elementsRetrieverTextarea, 0);
}, 'Focus to the first textarea', true);
key.setViewKey('M-p', function (ev) {
command.walkInputElement(command.elementsRetrieverButton, true, true);
}, 'Focus to the next button', false);
key.setViewKey('M-n', function (ev) {
command.walkInputElement(command.elementsRetrieverButton, false, true);
}, 'Focus to the previous button', false);
key.setCaretKey([["C-a"], ["^"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectBeginLine") : goDoCommand("cmd_beginLine");
}, 'Move caret to the beginning of the line', false);
key.setCaretKey([["C-n"], ["j"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectLineNext") : goDoCommand("cmd_scrollLineDown");
}, 'Move caret to the next line', false);
key.setCaretKey([["C-p"], ["k"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectLinePrevious") : goDoCommand("cmd_scrollLineUp");
}, 'Move caret to the previous line', false);
key.setCaretKey([["C-f"], ["l"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectCharNext") : goDoCommand("cmd_scrollRight");
}, 'Move caret to the right', false);
key.setCaretKey([["C-b"], ["h"], ["C-h"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectCharPrevious") : goDoCommand("cmd_scrollLeft");
}, 'Move caret to the left', false);
key.setCaretKey([["M-f"], ["w"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectWordNext") : goDoCommand("cmd_wordNext");
}, 'Move caret to the right by word', false);
key.setCaretKey([["M-b"], ["W"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectWordPrevious") : goDoCommand("cmd_wordPrevious");
}, 'Move caret to the left by word', false);
key.setCaretKey([["C-v"], ["SPC"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectPageNext") : goDoCommand("cmd_movePageDown");
}, 'Move caret down by page', false);
key.setCaretKey([["M-v"], ["b"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectPagePrevious") : goDoCommand("cmd_movePageUp");
}, 'Move caret up by page', false);
key.setCaretKey([["M-<"], ["g"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectTop") : goDoCommand("cmd_scrollTop");
}, 'Move caret to the top of the page', false);
key.setCaretKey([["M->"], ["G"]], function (ev) {
ev.target.ksMarked ? goDoCommand("cmd_selectEndLine") : goDoCommand("cmd_endLine");
}, 'Move caret to the end of the line', false);
key.setCaretKey('J', function (ev) {
util.getSelectionController().scrollLine(true);
}, 'Scroll line down', false);
key.setCaretKey('K', function (ev) {
util.getSelectionController().scrollLine(false);
}, 'Scroll line up', false);
key.setCaretKey(',', function (ev) {
util.getSelectionController().scrollHorizontal(true);
goDoCommand("cmd_scrollLeft");
}, 'Scroll left', false);
key.setCaretKey([["C-SPC"], ["C-@"]], function (ev) {
command.setMark(ev);
}, 'Set the mark', true);
key.setCaretKey(':', function (ev, arg) {
shell.input(null, arg);
}, 'List and execute commands', true);
key.setCaretKey('R', function (ev) {
BrowserReload();
}, 'Reload the page', true);
key.setCaretKey('B', function (ev) {
BrowserBack();
}, 'Back', false);
key.setCaretKey('F', function (ev) {
BrowserForward();
}, 'Forward', false);
key.setCaretKey(["C-x", "h"], function (ev) {
goDoCommand("cmd_selectAll");
}, 'Select all', true);
key.setCaretKey('f', function (ev) {
command.focusElement(command.elementsRetrieverTextarea, 0);
}, 'Focus to the first textarea', true);
key.setCaretKey('M-p', function (ev) {
command.walkInputElement(command.elementsRetrieverButton, true, true);
}, 'Focus to the next button', false);
key.setCaretKey('M-n', function (ev) {
command.walkInputElement(command.elementsRetrieverButton, false, true);
}, 'Focus to the previous button', false);
// {{ caret-mode
function toggleCaretModeFactory(backgroundColor, isCaretMode) {
return function(ev, arg) {
var i, children = document.getElementById("nav-bar").children;
for (i = 0; i < children.length; i++) {
children[i].style.backgroundColor = backgroundColor;
}
util.setBoolPref("accessibility.browsewithcaret", isCaretMode);
};
}
key.setViewKey(['C-c','C-i'], toggleCaretModeFactory('pink', true), 'Enter to caret mode', true);
key.setCaretKey(['C-c','C-i'], toggleCaretModeFactory(null, false), 'Leave from caret mode', true);
// }}
//{{%PRESERVE%
// {{ hok
hook.setHook('PluginLoaded', function () {
if (!plugins.hok) return;
/* HoK 1.3.9+ required */
plugins.hok.pOptions.selector = plugins.hok.pOptions.selector
/* feedly */
+ ", *[data-uri]" + ", *[data-selector-toggle]" + ", *[data-page-action]" + ", *[data-app-action]"
/* jira */
+ ", *[id='assignee-val']"
/* web apps developed by angular */
+ ", *[ng-click]" + ", *[ng-class]"
/* youtube */
+ ", *[contains(concat(' ', @class, ' '), ' ytp-menuitem ')]"
/* google plus */
+ ", *[guidedhelpid]"
/* http://bilibili.com */
+ ", *[contains(concat(' ', @class, ' '), ' bilibili-player-video-btn ')]"
+ ", *[contains(concat(' ', @class, ' '), ' bilibili-player-iconfont ')]"
/* http://zhihu.com */
+ ", *[contains(concat(' ', @class, ' '), ' zu-button-more ')]"
/* twitter */
+ ", *[data-item-count]";
});
key.setViewKey('e', function (aEvent, aArg) {
ext.exec("hok-start-foreground-mode", aArg);
}, 'Hok - Foreground hint mode', true);
key.setViewKey('E', function (aEvent, aArg) {
ext.exec("hok-start-background-mode", aArg);
}, 'HoK - Background hint mode', true);
key.setViewKey(';', function (aEvent, aArg) {
ext.exec("hok-start-extended-mode", aArg);
}, 'HoK - Extented hint mode', true);
key.setGlobalKey(['C-c','C-f'], function (aEvent, aArg) {
ext.exec("hok-start-foreground-mode", aArg);
}, 'Hok - Foreground hint mode', true);
key.setGlobalKey(['C-c','C-y'], function (aEvent, aArg) {
ext.exec("hok-yank-foreground-mode", aArg);
}, 'HoK - Background hint mode', true);
key.setGlobalKey(['C-c','C-;'], function (aEvent, aArg) {
ext.exec("hok-start-extended-mode", aArg);
}, 'HoK - Extented hint mode', true);
key.setGlobalKey(['C-c', 'C-e'], function (aEvent, aArg) {
ext.exec("hok-start-continuous-mode", aArg);
}, 'Start continuous HaH', true);
// }}
// {{ next/previous page
ext.add("previous-page", function () {
var document = window.content.document;
var links = document.links;
for(var i = 0; i < links.length; i++) {
if (/<?[ \t]*(上页|上一页|[Pp]revious|<|<<)/.test(links[i].text)) {
document.location = links[i].href;
return;
}
}
}, "Previous page");
ext.add("next-page", function () {
var document = window.content.document;
var links = document.links;
for(var i = 0; i < links.length; i++) {
if (/(下页|下一页|[Nn]ext|>|>>)[ \t]*>?/.test(links[i].text)) {
document.location = links[i].href;
return;
}
}
}, "Next page");
key.setViewKey([',', 'p', 'p'], function(ev, arg) {
ext.exec("previous-page", arg, ev);
}, "Previous page");
key.setViewKey([',', 'n', 'n'], function(ev, arg) {
ext.exec("next-page", arg, ev);
}, "Next page");
// }}
// my own snippets
key.setGlobalKey(['C-c', 'C-l'], function(ev, arg) {
// window.content will get the top most window (current focus could be in a iframe)
var document = window.content.document,
link,
rlt = '',
r,
regexp;
prompt.reader({
message : "Regex to filter link URLs on this page:",
// collection : collection,
flags : [0 , 0],
// style : [null, style.prompt.description],
header : ["Title", "URL"],
width : [25, 45],
onChange: function (arg) {
r= arg.textbox.value;
},
onFinish:function (arg) {
if(r && r !=='') {
regexp= new RegExp(r, 'g');
}
for(var i = 0; i < document.links.length; i++) {
link = document.links[i].href;
if(!regexp) {
rlt += link + '\n';
} else {
if(link.match(regexp)) {
rlt += link + '\n';
}
}
}
command.setClipboardText(rlt);
}
});
}, "Copy all the links to clipboard");
// {{ Scrollet
key.setGlobalKey(['C-x', 'r', 'SPC'], function (ev, arg) {
ext.exec("scrollet-set-mark", arg, ev);
}, "Save current scroll position to the mark", true);
key.setGlobalKey(['C-x', 'r', 'j'], function (ev, arg) {
ext.exec("scrollet-jump-to-mark", arg, ev);
}, "Jump to the saved position", true);
// }}
// {{ find tab API
function findTabFactory (fn) {
return function(ev, arg) {
var tabItems = (function() {
var tabs = Array.slice(gBrowser.mTabContainer.childNodes), rlt = [], tab, url;
for (var i = 0; i < tabs.length; i++) {
tab = tabs[i];
url = tab.linkedBrowser.contentWindow.location.href;
rlt.push([util.getFaviconPath(url), tab.label, url, tab]);
}
return rlt;
})();
prompt.selector({
message : "Select tab: ",
initialIndex : gBrowser.mTabContainer.selectedIndex,
flags : [ICON | IGNORE, 0, 0, IGNORE | HIDDEN],
collection : tabItems,
header : ["Title", "URL"],
keymap : {},
supressRecoverFocus : true,
callback: function (i) {
var tab;
if (i >= 0) {
tab = tabItems[i][3];
}
fn.call(this, tabItems, i);
},
onFinish : function() {
gBrowser.focus();
_content.focus();
}
});
};
}
// }}
// {{ tiletab
key.setViewKey([',', 'x', 's'], function (ev, arg) {
tileTabs.menuActions('save',-1);
}, 'Save tile layout');
key.setViewKey([',', 'x', 'o'], function (ev, arg) {
tileTabs.menuActions('open',-1);
}, 'Open tile layout');
function splitTabFactory(tileTabKeyword) {
return findTabFactory(function(tabItems, index) {
var tab = tabItems[index][3];
if(tab) {
tileTabs.tileActions(tileTabKeyword, tab);
}
/* else maybe we just create a new tab */
});
}
key.setViewKey([',', 'x', '2'], splitTabFactory('tilelink-above'), 'Split horizontally with clicked tab');
key.setViewKey([',', 'x', '3'], splitTabFactory('tilelink-left'), 'Split vertically with clicked tab');
key.setGlobalKey(['C-x', '2'], splitTabFactory('tilelink-above'), 'Split horizontally with current selected tab');
key.setGlobalKey(['C-x', '3'], splitTabFactory('tilelink-left'), 'Split vertically with current selected tab');
key.setGlobalKey(['C-x', '0'], function (ev, arg) {
tileTabs.menuActions('untile',-1);
}, 'Close tile');
key.setGlobalKey(['C-x', '1'], function (ev, arg) {
tileTabs.menuActions('close',null);
}, 'One tile');
key.setGlobalKey(['C-x', 'o'], function (ev, arg) {
function findLayoutByTab(tab) {
var i;
for (i = 0; i < tileTabs.layouts.length; i++) {
if (tileTabs.layouts[i].layoutID == tab.getAttribute("tiletabs-assigned"))
return tileTabs.layouts[i];
}
return null;
}
var tabs = gBrowser.tabs;
function getBeFocusedTabByIndex(startIndex, selectedTabLayout) {
for (var i = startIndex; i < tabs.length; i++) {
var tabLayout = findLayoutByTab(tabs[i]);
if (!tabLayout) {
continue;
}
if (tabLayout.layoutID === selectedTabLayout.layoutID) {
return tabs[i];
}
}
return null;
}
var selectedTabLayout = findLayoutByTab(gBrowser.selectedTab);
if (!selectedTabLayout) {
return;
}
var currentTabIndex;
for (var i = 0; tabs.length; i++) {
if (tabs[i] === gBrowser.selectedTab) {
currentTabIndex = i;
break;
}
}
var tab = getBeFocusedTabByIndex(currentTabIndex + 1, selectedTabLayout);
if (tab) {
gBrowser.selectedTab = tab;
} else {
tab = getBeFocusedTabByIndex(0, selectedTabLayout);
gBrowser.selectedTab = tab;
}
}, 'Select next tile', true);
// }}
function openAnyTabFactory () {
return findTabFactory(function(tabItems, index) {
if(index < 0) return;
gBrowser.mTabContainer.selectedIndex = index;
});
}
key.setViewKey([',', 'x', 'b'], openAnyTabFactory(), 'View all tabs', true);
key.setGlobalKey(['C-x', 'b'], openAnyTabFactory(), 'View all tabs', true);
function closeAllExceptCurrentTabFactory () {
return findTabFactory(function(tabItems, index) {
var i, item = tabItems[index];
for (i = 0; i < tabItems.length; ++i)
{
if (i !== index)
gBrowser.removeTab(tabItems[i][3]);
}
});
}
key.setViewKey([',', 'x', 'a'], closeAllExceptCurrentTabFactory(), 'Close all tabs execept selected one', true);
key.setGlobalKey(['C-x', 'C-a'], closeAllExceptCurrentTabFactory(), 'Close all tabs except seleted one', true);
// }}
function searchHistory(evt, arg) {
function timeSince(now, date) {
var seconds = Math.floor((now - date) / 1000);
var interval = Math.floor(seconds / 31536000);
if (interval > 1) {
return interval + " years";
}
interval = Math.floor(seconds / 2592000);
if (interval > 1) {
return interval + " months";
}
interval = Math.floor(seconds / 86400);
if (interval > 1) {
return interval + " days";
}
interval = Math.floor(seconds / 3600);
if (interval > 1) {
return interval + " hours";
}
interval = Math.floor(seconds / 60);
if (interval > 1) {
return interval + " minutes";
}
return Math.floor(seconds) + " seconds";
}
function searchWithKeyword(q) {
var collection = (function() {
//search option
var options = PlacesUtils.history.getNewQueryOptions();
options.maxResults = 4096;
options.queryType = Ci.nsINavHistoryQueryOptions.QUERY_TYPE_HISTORY;
//options.sortingMode = Ci.nsINavHistoryQueryOptions.SORT_BY_FRECENCY_DESCENDING;
options.sortingMode = Ci.nsINavHistoryQueryOptions.SORT_BY_DATE_DESCENDING;
options.includeHidden = true;
//search query
var query = PlacesUtils.history.getNewQuery();
// read keyworld
if(q && q !== '') {
query.searchTerms = q;
}
var result = PlacesUtils.history.executeQuery(query, options);
var root = result.root;
var collection = [];
var now = new Date().getTime();
var siteNode;
root.containerOpen = true;
for (var i = 0; i < root.childCount; i++) {
// siteNode => nsINavHistoryResultNode
siteNode = root.getChild(i);
collection.push([siteNode.icon,siteNode.title,siteNode.uri, siteNode.time/1000]);
}
collection.sort(function(a, b) {
return b[3]-a[3];
});
// reformat the time
for (i = 0; i < collection.length; i++) {
collection[i][3] = timeSince(now, collection[i][3]) + ' ago';
}
root.containerOpen = false;
return collection;
})();
prompt.selector({
message : "Search history"+ (q && q !== ''? (' @'+q +':') : ':' ),
collection : collection,
flags : [ICON | IGNORE, 0, 0, 0],
header : ["Title", "Url", "Last visited"],
width : [30, 60, 10],
callback: function (i) {
if (i >= 0) {
openUILinkIn(collection[i][2], "tab");
}
},
onFinish: function() {
gBrowser.focus();
_content.focus();
}
});
}
prompt.finish(true);
prompt.read('Keyword to search history?', searchWithKeyword, null, null, null, 0, "history_search");
// searchWithKeyword('test');
}
key.setViewKey([',', 'h', 'h'], searchHistory, "Search history");
key.setGlobalKey(['C-c', 'C-h'], searchHistory, "Search history");
// {{ pinboard
function pinboardAddBookmark (evt, arg) {
var document = window.content.document;
window.open('https://pinboard.in/add?showtags=yes&url='+encodeURIComponent(document.location.href)+'&description='+encodeURIComponent(document.getSelection?document.getSelection():'')+'&title='+encodeURIComponent(document.title),'Pinboard','toolbar=no,scrollbars=yes,width=750,height=700');
}
key.setViewKey([',', 'x', 'd'], pinboardAddBookmark, 'Pinboard: add bookmark');
key.setGlobalKey(['C-x', 'C-d'], pinboardAddBookmark, 'Pinboard: add bookmark');
function pinboardViewBookmarks (evt, arg) {
// @see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/Feed_content_access_API
function pinboardSearch (tagInput) {
var feedUrl = 'http://feeds.pinboard.in/rss/secret:e46a5e6d2145babbf1fc/u:redguardtoo/t:'+ tagInput;
var httpRequest = null;
function infoReceived() {
var data = httpRequest.responseText;
var ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Components.interfaces.nsIIOService);
var uri = ioService.newURI(feedUrl, null, null);
if (data.length) {
var parser = Components.classes["@mozilla.org/feed-processor;1"].createInstance(Components.interfaces.nsIFeedProcessor);
try {
// actual data processing
parser.listener = {
handleResult: function(result) {
var feed = result.doc;
feed.QueryInterface(Components.interfaces.nsIFeed);
if (!feed.items.length) {
return;
}
var formttedFeedItems = (function(feedItems) {
var collection = [], i, theEntry;
for (i=0; i < feedItems.length; i++) {
theEntry = feedItems.queryElementAt(i, Components.interfaces.nsIFeedEntry);
if (theEntry) {
collection.push([theEntry.title.text, theEntry.link.resolve("")]);
}
}
return collection;
})(feed.items);
prompt.selector({
message : 'Search pinboard bookmarks @'+tagInput,
collection : formttedFeedItems,
flags : [0, 0],
header : ["Title", "Url"],
width : [30, 70],
callback: function (i) {
if (i >= 0) {
openUILinkIn(formttedFeedItems[i][1], "tab");
}
},
onFinish: function() {
gBrowser.focus();
_content.focus();
}
});
}
};
parser.parseFromString(data, uri);
}
catch(e) {
alert("Error parsing feed. Error=", e);
}
}
}
httpRequest = new XMLHttpRequest();
httpRequest.open("GET", feedUrl, true);
try {
httpRequest.onload = infoReceived;
httpRequest.send(null);
} catch(e) {
alert('Failed to fetch feed. Error=', e);
}
}
prompt.finish(true);
prompt.read('Keyword(s) for pinboard? Multiple keywords seperated by space', pinboardSearch, null, null, null, 0, "pinboard_search");
}
key.setViewKey([',', 'v', 'v'], pinboardViewBookmarks, 'Pinboard: view bookmarks');
key.setGlobalKey(['C-c', 'C-v'], pinboardViewBookmarks, 'Pinboard: view bookmarks');
// }}
// {{ get verycd resource
function verycdDownloadPage (evt, arg) {
var url = window.content.document.location + '';
var dstURL = url.replace('verycd.com/','verycd.gdajie.com/');
openUILinkIn(dstURL, "tab");
}
key.setViewKey([',', 'v', 'c'], verycdDownloadPage, 'Open verycd resoure page.');
// }}
// {{ hide QUORA "popular" stories
function purgeQuora (evt, arg) {
var url = window.content.document.location + '';
if(!url.match(/quora\.com/)) {
return;
}
// also add below link to the bookmark
// javascript:a=Array.from(document.getElementsByClassName("AnswerStoryToggleModal"));a.forEach(function(e){e.style.display=a[a.length-1].style.display==='none'?'block':'none';});
var a=Array.from(window.content.document.getElementsByClassName("AnswerStoryToggleModal"));
a.forEach(function(e){
e.style.display=a[a.length-1].style.display==='none'?'block':'none';
});
}
key.setGlobalKey(['C-c', 'q'], purgeQuora, 'Quora purged');
// }}
//}}%PRESERVE%
// Local Variables:
// coding: utf-8
// tab-width: 2
// js-indent-level: 2
// js2-basic-offset: 2
// End:
// vim: set fs=javascript fenc=utf-8 et ts=2 sts=2 sw=2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment