-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuttlr.user.js
executable file
·146 lines (132 loc) · 4.21 KB
/
muttlr.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// ==UserScript==
// @name Muttlr
// @description a tumblr suite that improves the site
//
// @include https://www.tumblr.com/dashboard*
// @include https://tumblr.com/dashboard*
// @include https://www.tumblr.com/tumblelog/*
// @include https://tumblr.com/tumblelog/*
// @include https://tumblr.com/likes
// @exclude https://www.tumblr.com/drafts
// @exclude https://www.tumblr.com/messages
// @exclude https://www.tumblr.com/queue
// @exclude https://tumblr.com/drafts
// @exclude https://tumblr.com/messages
// @exclude https://tumblr.com/queue
// @exclude https://www.tumblr.com/tumblelog/*/drafts
// @exclude https://www.tumblr.com/tumblelog/*/messages
// @exclude https://www.tumblr.com/tumblelog/*/queue
// @exclude https://tumblr.com/tumblelog/*/drafts
// @exclude https://tumblr.com/tumblelog/*/messages
// @exclude https://tumblr.com/tumblelog/*/queue
//
// @namespace http://sqnya.se
// @version 0.1
// @date 2015-12-01
// @creator parulina
// ==/UserScript==
// http://stackoverflow.com/a/23259289
var timeSince = function(date) {
if (typeof date !== 'object') {
date = new Date(date);
}
var seconds = Math.floor((new Date() - date) / 1000);
var intervalType;
var interval = Math.floor(seconds / 31536000);
if (interval >= 1) {
intervalType = 'year';
} else {
interval = Math.floor(seconds / 2592000);
if (interval >= 1) {
intervalType = 'month';
} else {
interval = Math.floor(seconds / 86400);
if (interval >= 1) {
intervalType = 'day';
} else {
interval = Math.floor(seconds / 3600);
if (interval >= 1) {
intervalType = "hour";
} else {
interval = Math.floor(seconds / 60);
if (interval >= 1) {
intervalType = "minute";
} else {
interval = seconds;
intervalType = "second";
}
}
}
}
}
if (interval > 1 || interval === 0) {
intervalType += 's';
}
return interval + ' ' + intervalType;
};
var Muttlr = new function(){
var pthis = this;
this.key = "fuiKNFp9vQFvjLNvx4sUwti4Yb5yGutBN4Xh10LXZhhRKjWlV4";
this.getPostInfo = function(post, callback){
if(!post) return null;
var id = post.getAttribute("data-post-id"),
blog = post.getAttribute("data-tumblelog-name");
var blogurl = blog + ".tumblr.com";
var url = "https://api.tumblr.com/v2/blog/" + blogurl + "/posts?callback=?&api_key=" + pthis.key + "&id=" + id;
jQuery.getJSON(url, function(e){
if(e.meta.status == 200) callback(e.response);
});
}
this.setTimestampStorage = function(id, ts){
var saved_timestamps = {};
if(localStorage.post_timestamps) {
saved_timestamps = JSON.parse(localStorage.post_timestamps);
}
saved_timestamps[id] = ts;
var keys = Object.keys(saved_timestamps).sort(),
limit = 50;
if(keys.length > limit){
for(var i = 0; i < (keys.length - limit); i++){
delete saved_timestamps[keys[0]];
delete keys[0];
}
}
localStorage.post_timestamps = JSON.stringify(saved_timestamps);
}
this.addPostDateHeader = function(post, date){
if(jQuery(post).hasClass("timestamped")) return;
var timestr = new Date(date).toISOString().replace("T", " ").replace(/\..*/, "");
timestr += " -- " + timeSince(date) + " ago";
var d = jQuery("<div>", {text: timestr, style: "padding:0 20px; font-size:12px;"});
d.insertBefore(jQuery(post).find(".post_header"));
jQuery(post).addClass("timestamped");
}
this.updatePosts = function(){
var saved_timestamps = {};
if(localStorage.post_timestamps) {
saved_timestamps = JSON.parse(localStorage.post_timestamps);
}
jQuery("#posts .post_full:not(.new_post):not(.timestamped)").each(function(k, post){
var id = post.getAttribute("data-post-id");
var date = saved_timestamps[id];
if(!date){
pthis.getPostInfo(post, function(e){
if(e.total_posts == 1){
var p = e.posts[0];
pthis.setTimestampStorage(p.id, p.timestamp * 1000);
pthis.addPostDateHeader(post, p.timestamp * 1000);
}
});
}
if(date) {
pthis.addPostDateHeader(post, date);
}
});
};
};
window.addEventListener("load", function(){
Muttlr.updatePosts();
});
jQuery("#posts").on("mouseenter", function(){
Muttlr.updatePosts();
});