Skip to content

Commit

Permalink
added go back in history, open in new tab and close current tab keys
Browse files Browse the repository at this point in the history
  • Loading branch information
greg-js committed Oct 5, 2016
1 parent 68d120b commit 9af5599
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
42 changes: 31 additions & 11 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Receive URI messages from content.js and navigate current tab
* Receive URI messages from content.js and route to right function
*/
chrome.runtime.onMessage.addListener(function(req) {
var msg;
Expand All @@ -8,16 +8,13 @@ chrome.runtime.onMessage.addListener(function(req) {
if (req.from === 'content.js') {
msg = req.message;

// redirect goto hints
if (msg.type === 'goto') {
chrome.tabs.query({
currentWindow: true,
active: true
}, function(tab) {
chrome.tabs.update(tab.id, {
url: msg.payload
});
});
// close current tab
if (msg.type === 'close') {
closeTab();

// open link in new window
} else if (msg.type === 'link') {
openNewTab(msg.payload);

// switch tabs
} else if (msg.type === 'tabs') {
Expand All @@ -26,6 +23,29 @@ chrome.runtime.onMessage.addListener(function(req) {
}
});

/**
* Open a given url in a new window
* @param {string} url - the url
*/
function openNewTab(url) {
chrome.tabs.create({
url: url
});
}

/**
* Close the current tab
*/
function closeTab() {
// get active tab
chrome.tabs.query({
currentWindow: true,
active: true
}, function(activeTab) {
chrome.tabs.remove(activeTab[0].id);
});
}

/**
* Switch to the next or previous tab in the current window
* @param {number} mod - the modifier (1 or -1)
Expand Down
29 changes: 23 additions & 6 deletions content.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ document.addEventListener('keyup', handleInsertKey, true);
*/
function handleNavKeys(e) {
// disable the navigational keys if in an input field
if (h.isEditable(e.target) || e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) {
if (h.isEditable(e.target) || e.ctrlKey || e.metaKey || e.altKey) {
return void 0;
}

Expand All @@ -50,7 +50,11 @@ function handleNavKeys(e) {
window.scrollBy(0, -window.innerHeight/2);
break;
case 8: // backspace
window.history.go(-1);
if (e.shiftKey) {
window.history.go(1);
} else {
window.history.go(-1);
}
break;
case 78: // 'n'
h.sendMessage({
Expand All @@ -64,6 +68,10 @@ function handleNavKeys(e) {
payload: -1
});
break;
case 81: // 'q'
h.sendMessage({
type: 'close'
});
}
}

Expand All @@ -73,7 +81,7 @@ function handleNavKeys(e) {
*/
function handleLinkKey(e) {
// disable in input fields
if (h.isEditable(e.target) || e.ctrlKey || e.shiftKey || e.metaKey || e.altKey) {
if (h.isEditable(e.target) || e.shiftKey || e.ctrlKey || e.metaKey || e.altKey) {
return void 0;
}

Expand Down Expand Up @@ -138,9 +146,18 @@ function captureKeyboard(e) {

// look for hits in the existing hints links
if (state.hints[last]) {
state.hints[last].el.click();
state.hints[last].el.focus();
resetCapture();
if (e.shiftKey && state.isLinkHinting) {
// send to background as an action to open in new tab
h.sendMessage({
type: 'link',
payload: state.hints[last].el.href
});
// just simulate a click & focus
} else {
state.hints[last].el.click();
state.hints[last].el.focus();
resetCapture();
}
}
}
}
Expand Down

0 comments on commit 9af5599

Please sign in to comment.