"Durov, Verni stenu" or "Habrahabr + Geektimes + Megamind" in a single tape



When raged Roskomnadzor and Habr internally divided reasons, appeared "younger brother", which is not a pity — Geektimes. I such a division had not like and I made for local use in small browser extension that displays posts Geektimes habré in a single tape.

Yesterday happened another division, Habra you receive a "cousin Humanities" — Megamind. To add the ribbon General ribbon on habré I was not difficult. Seeing the mood of all these divisions, I decided to put this extension in the General access, perhaps You will also be more comfortable to read Habr (the old way).


only works on the tabs:

For was "everyone" — http://habrahabr.ru/feed/all/
For unregistered "best of day" — http://habrahabr.ru/posts/top/daily/

The job is really simple, when entering, is two additional GET request geektimes and megamozg, get the posts and are inserted between existing habré, sorted by time.

Sync with geektimes quite nicely everything is displayed, but megamozg still have problems, due to the fact that there are quite a few posts a day, soon I think things will change.

.:



Who is interested in the source code, here:

Open the code
var habr = {

init: function() {

var last = document.createElement('div'),
published = document.createElement('div');
last.setAttribute('id', 'last_id');
last.setAttribute('class', 'post shortcuts_item');
last.setAttribute('style', 'display:none;');
published.setAttribute('class', 'published');
published.innerText = '20 January 2015 at 00:01';
last.appendChild(published);
document.getElementsByClassName('posts')[0].appendChild(last);

habr.feed('geektimes.ru');

},

feed: function(host) {

var href = location.href;
href = href.replace('habrahabr.ru/posts/top/daily/',host + '/interesting/');
href = href.replace('habrahabr.ru/feed',host);

var xhr = new XMLHttpRequest();
xhr.open("GET", href, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {

var html = document.createElement('div');
html.innerHTML = xhr.responseText;
var posts = html.getElementsByClassName('post');

for (var i = 0; i < posts.length; i++) {

var post = document.createElement('div');
post.innerHTML = posts[i].innerHTML;

var date = post.getElementsByClassName('published')[0].innerText;

var postDate = habr.getDate(date);

post.getElementsByClassName('title')[0].setAttribute('style','padding-right:71px;');
post.getElementsByClassName('favorite')[0].parentNode.removeChild(post.getElementsByClassName('favorite')[0]);
post.getElementsByClassName('plus')[0].parentNode.removeChild(post.getElementsByClassName('plus')[0]);
post.getElementsByClassName('minus')[0].parentNode.removeChild(post.getElementsByClassName('minus')[0]);

habr.addChild(post.innerHTML, postDate, host, posts[i].id);

if (i == posts.length-1 && host == 'geektimes.ru') {
habr.feed('megamozg.ru');
}

}

}
};
xhr.send(null);

},

addChild: function(data, time, host, id) {

var posts = document.getElementsByClassName('post');

for (var i = 0; i < posts.length; i++) {

var date = posts[i].getElementsByClassName('published')[0].innerText;

var postDate = habr.getDate(date);

var postAdd = document.createElement('div');
postAdd.setAttribute('class', 'post shortcuts_item');
postAdd.setAttribute('id', id);
postAdd.setAttribute('style', 'background:url("http://' + host + '/images/logo.svg") right top no-repeat; background-size: 71px;');
postAdd.innerHTML = data;

if (time >= postDate) {
document.getElementsByClassName('posts')[0].insertBefore(postAdd, document.getElementById(posts[i].id));
break;
}

}

},

getDate: function(date) {

var d = new Date(),
dateYear,
dateMonth,
dateDay,
dateHour,
dateMin,
dateMinSec,
parseDate;

if (date.indexOf('today') + 1) {

date = date.replace('today at ', ");
dateMinSec = date.split(':');

dateYear = d.getFullYear();
dateMonth = d.getMonth();
dateDay = d.getDate();
dateHour = dateMinSec[0];
dateMin = dateMinSec[1];

}
else  if  (date.indexOf('yesterday') + 1) {

date = date.replace('yesterday at ', ");


dateYear = d.getFullYear();
dateMonth = d.getMonth();
dateDay = d.getDate()-1;
dateHour = dateMinSec[0];
dateMin = dateMinSec[1];

}
else {

parseDate = /([0-9]{1,2})\s(yanvarya|fevralya|Marta|Aprilia|mA|yuna|Yulia|August|sentyabrya|Oktyabrya|noyabrya|dekabria)\s([0-9]{4})\b\s([0-9]{2}):([0-9]{2})/gi.exec(date);

if (!parseDate) {
parseDate = /([0-9]{1,2})\s(yanvarya|fevralya|Marta|Aprilia|mA|yuna|Yulia|August|sentyabrya|Oktyabrya|noyabrya|dekabria)\V\s([0-9]{2}):([0-9]{2})/gi.exec(date);
dateYear = d.getFullYear();
dateDay = parseDate[1];
dateHour = parseDate[3];
dateMin = parseDate[4];
}
else {
dateYear = parseDate[3];
dateDay = parseDate[1];
dateHour = parseDate[4];
dateMin = parseDate[5];
}

switch (parseDate[2]) {
case 'Jan':
dateMonth = 0;
break;
case 'Feb':
dateMonth = 1;
break;
case 'Mar':
dateMonth = 2;
break;
case 'APR':
dateMonth = 3;
break;
case 'may':
dateMonth = 4;
break;
case 'Jun':
dateMonth = 5;
break;
case 'July':
dateMonth = 6;
break;
case 'August':
dateMonth = 7;
break;
case 'September':
dateMonth = 8;
break;
case 'Oct':
dateMonth = 9;
break;
case 'Nov':
dateMonth = 10;
break;
case 'Dec':
dateMonth = 11;
break;
default :
dateMonth = 0;
}

}

return new Date(dateYear, dateMonth, dateDay, dateHour, dateMin);

}

};

habr.init();



PS: the Author is extensionsapp not in the Club of Cheerful and Resourceful, because who is generous invites, always will be glad to receive it and one to tell you about one pretty interesting extension.
Thank you, ragequit.

Source code on GitHub:
github.com/extensionsapp/habrahabr-chrome
github.com/extensionsapp/habrahabr-firefox

UPD 13.03.15: Changed the link to Habre. Everything was fixed and working.
Now working on 3 links:
http://habrahabr.ru/feed/interesting/ — interesting things from Your subscriptions.
http://habrahabr.ru/feed/all/ all of Your subscriptions.
http://habrahabr.ru/all/ is everything.

If you want to read Habrahabr and Geektimes, go to Megamind and remove tick from all hubs. Similar manipulations for the Humanities.

UPD 09.09.15:
Install monkey:
Violent monkey [Opera], Greasemonkey [Firefox], Tampermonkey [Chrome], GreaseKit [Safari]

Then install the extension:
Click Row -> https://gist.github.com/extensionsapp/946a14d5c820d57ac7c4
or
Make Install -> https://openuserjs.org/scripts/extensionsapp/habrahabr-userscript

UPD 16.08.17: Update script after the redesign Habra.
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Automatically create Liquibase migrations for PostgreSQL

Vkontakte sync with address book for iPhone. How it was done

What part of the archived web