Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NewsBlur - Use Google Reader keys in list view
// ==UserScript==
// @name NewsBlur - Use Google Reader keys in list view
// @namespace https://github.com/toupeira
// @include *newsblur.com/*
// @version 1.1
// ==/UserScript==
// Based on http://userscripts.org/scripts/show/163853 by George Dorn. Wait, that name sounds familiar...
/* global GM_openInTab */
/* global unsafeWindow */
var $ = unsafeWindow.$;
var NB = unsafeWindow.NEWSBLUR;
if (typeof $ == 'undefined' || typeof NB == 'undefined') {
return;
}
function bind_key(event, char, callback) {
document.documentElement.addEventListener('key'+event, function(e) {
try {
if (e.altKey || e.ctrlKey || e.metaKey || $('*:focus').size() > 0) {
return;
}
var codes = [ char.toLowerCase().charCodeAt(), char.toUpperCase().charCodeAt() ];
var code = e.keyCode;
if (code === 0) {
code = e.which;
}
if (codes.indexOf(code) > -1) {
e.preventDefault();
callback(event);
}
} catch(e) {
alert("Error in user script: " + (e.message || e));
console.log(e);
}
}, true);
}
function navigate_to_story(direction) {
// don't mark story automatically as read
var current_delay = NB.assets.preference('read_story_delay');
unsafeWindow.setTimeout(function() {
try {
NB.assets.preference('read_story_delay', -1);
var story_titles = $('#story_titles');
var current_scroll = story_titles[0].scrollTop;
NB.reader.show_next_story(direction);
if ($('#story_pane').is(':hidden')) {
$('.NB-feed-story').hide();
story_titles[0].scrollTop = current_scroll;
var top = $('.NB-feed-story').parents('.NB-story-detail:first').prev('.NB-story-title').position().top;
if (top < 60) {
story_titles[0].scrollTop -= 25;
} else if (top > story_titles.height() - 100) {
story_titles[0].scrollTop += 25;
}
}
} finally {
// reset the original preference
NB.assets.preference('read_story_delay', current_delay);
}
}, 0);
}
function mark_active_story_read() {
var story = NB.reader.active_story;
if (!story.attributes.read_status && NB.assets.preference('read_story_delay') > -1) {
NB.reader.mark_active_story_read();
}
}
try {
// unbind existing shortcuts
$(document).ready(function() {
var keys_to_unbind = [ 'up', 'down', 'n', 'p', 'o', 'v' ];
// collect handlers to unbind; can't modify during iteration as $._data is realtime
var handlers = {
keydown: [],
keyup: [],
keypress: []
};
$.each([ 'keydown', 'keyup', 'keypress' ], function(i, event) {
var events = $._data(document, 'events')[event];
// collect the ones matching the keys we don't like
for (i = 0; i < events.length; i++) {
var obj = events[i];
if (keys_to_unbind.indexOf(obj.data) > -1) {
handlers[event].push(obj);
}
}
});
$.each(handlers, function(event, handlers) {
$.each(handlers, function(i, obj) {
$(document).unbind(event, obj.handler);
});
});
// rebind n and p to what up/down used to do
bind_key('down', 'n', function() {
navigate_to_story(1);
});
bind_key('down', 'p', function() {
navigate_to_story(-1);
});
// bind 'o' to expand/collapse a story
bind_key('down', 'o', function() {
if ($('#story_pane').is(':hidden')) {
var content = $('.NB-feed-story');
if (content.is(':visible')) {
content.hide();
} else {
content.show();
unsafeWindow.setTimeout(mark_active_story_read, 0);
}
}
});
// bind 'v' to open story in a new tab/window
bind_key('press', 'v', function() {
var story = NB.reader.active_story;
if (story) {
GM_openInTab(story.story_permalink, true);
mark_active_story_read();
}
});
});
} catch(e) {
alert("Error loading plugin, Google Reader keys are disabled (" + (e.message || e) + ")");
console.log(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.