initial upload

This commit is contained in:
Kerrick Long
2011-06-16 12:43:14 -05:00
parent 4cd705344e
commit 1e460a77b2
8 changed files with 550 additions and 0 deletions
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 448 B

+114
View File
@@ -0,0 +1,114 @@
<script>
var reddit_session = null;
setBadgeDefaults();
window.localStorage.clear();
chrome.tabs.onUpdated.addListener(listenToTabs);
chrome.tabs.onRemoved.addListener(cleanCache);
chrome.cookies.get({url: 'http://reddit.com', name: 'reddit_session'}, function(cookie){
reddit_session = cookie.value;
console.log('cookie.value = ' + cookie.value);
});
function setBadgeDefaults(tabId) {
chrome.browserAction.setBadgeBackgroundColor({
color: [192,192,192,255] //r,g,b,a
});
chrome.browserAction.onClicked.removeListener(submitToReddit);
if(tabId) {
chrome.browserAction.setBadgeText({
text: '?',
tabId: tabId
});
chrome.browserAction.setTitle({
title: 'Refresh the page to load data.',
tabId: tabId
});
} else {
chrome.browserAction.setBadgeText({
text: '?',
});
chrome.browserAction.setTitle({
title: 'Refresh the page to load data.'
});
}
}
function cleanCache() {
// find old caches from localStorage and remove anything older than ___.
}
function listenToTabs(tabId,changeInfo,tab){
if(changeInfo.status === 'loading') {
grabData(tab.url,tabId);
}
}
function grabData(url,tabId) {
//check if the url has been cached recently
if(window.localStorage.getItem(url) === null || JSON.parse(window.localStorage.getItem(url)).cachetime - new Date < -60000) {
console.log('Loading from reddit api...');
var reqUrl = 'http://www.reddit.com/api/info.json?url=' + encodeURI(url);
var api = new XMLHttpRequest();
api.open('GET',reqUrl,false);
api.send(null);
if(api.status !== 200) {
console.error('Error loading API.\nURL: ' + reqUrl + '\nStatus: ' + api.status);
console.log(api);
setBadgeDefaults(tabId);
}
api.onload = prepareButton(JSON.parse(api.responseText),url,tabId);
} else {
console.log('Loading from cache...');
prepareButton(JSON.parse(window.localStorage.getItem(url)).response,url,tabId);
}
}
function prepareButton(response,pageUrl,tabId) {
// add response to cache to reduce API calls
if(window.localStorage.getItem(pageUrl) === null || JSON.parse(window.localStorage.getItem(pageUrl)).cachetime - new Date < -60000) {
var toCache = new Object();
toCache.cachetime = new Date();
toCache.response = response;
window.localStorage.setItem(pageUrl,JSON.stringify(toCache));
delete toCache;
}
// counts submissions and prepare the browserAction button appropriately
var numberOfSubmissions = response.data.children.length.toString();
if(numberOfSubmissions > 0) {
chrome.browserAction.setTitle({
title: 'This page has been submitted to reddit ' + numberOfSubmissions + ' times.',
tabId: tabId
});
chrome.browserAction.setBadgeText({
text: numberOfSubmissions,
tabId: tabId
});
chrome.browserAction.setPopup({
popup: 'popup.html',
tabId: tabId
});
chrome.browserAction.setBadgeBackgroundColor({
color: [255,69,0,255], //r,g,b,a,
tabId: tabId
});
chrome.browserAction.onClicked.removeListener(submitToReddit);
} else {
chrome.browserAction.setTitle({
title: 'Submit this page to reddit',
tabId: tabId
});
chrome.browserAction.setBadgeText({
text: '',
tabId: tabId
});
chrome.browserAction.setPopup({
popup: '',
tabId: tabId
});
chrome.browserAction.onClicked.removeListener(submitToReddit);
chrome.browserAction.onClicked.addListener(submitToReddit);
}
}
function submitToReddit(tab){
chrome.tabs.create({
url: 'http://www.reddit.com/submit?url=' + encodeURI(tab.url)
});
}
</script>
Binary file not shown.

After

Width:  |  Height:  |  Size: 174 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 138 B

+14
View File
@@ -0,0 +1,14 @@
{
"name": "Best Reddit Check",
"version": "0.1",
"description": "Easily see how many times URL has been posted to reddit, join the discussion, submit or repost it, and more!",
"browser_action": {
"default_icon": "alien.png"
},
"permissions": [
"http://*.reddit.com/",
"tabs",
"cookies"
],
"background_page": "background.html"
}
+392
View File
@@ -0,0 +1,392 @@
<!doctype html>
<html>
<head>
<title>TESTING the title</title>
<meta charset="utf-8">
<style>
body {
min-width: 700px;
overflow-x: hidden;
overflow-y: auto;
font: normal x-small verdana, arial, helvetica, sans-serif;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
#posts {
list-style-type: none;
margin:0;
padding:0;
}
#posts > li:before {
content: counter(postCount) " ";
counter-increment: postCount;
float: left;
margin-top: 15px;
color: #C6C6C6;
font-size: medium;
text-align: right;
font-family: arial;
}
#posts:first-child {
counter-reset: postCount;
}
#posts li {
margin: 0;
padding: 0;
padding-left: 3px;
margin-bottom: 8px;
clear:both;
overflow:auto;
}
li.hidden .votes, li.hidden .thumblink, li.hidden .link, li.hidden .domain, li.hidden .meta, li.hidden .actions *:not(.hide){
opacity: 0.4;
}
.votes {
float: left;
margin-right: 4px;
margin-left: 7px;
font-size: small;
width: 5ex;
overflow: hidden;
font-weight: bold;
}
.upmod, .downmod {
margin: 2px 0px 0px 0px;
width: 100%;
height: 14px;
display: block;
cursor: pointer;
background-position: center center;
background-repeat: no-repeat;
width: 15px;
margin-left: auto;
margin-right: auto;
background-image: url('sprite.png');
}
.upmod {
background-position: -4px -271px;
}
li[data-dir='1'] .upmod {
background-position: -4px -227px;
}
.downmod {
background-position: -4px -293px;
}
li[data-dir='-1'] .downmod {
background-position: -4px -249px;
}
.count {
text-align: center;
color: #C6C6C6;
display: block;
}
.thumblink {
float: left;
margin: 0px 5px;
overflow: hidden;
width: 70px;
}
.post {
overflow: hidden;
margin-left: 3px;
}
.link {
font-size: medium;
font-weight: normal;
margin-bottom: 1px;
}
.domain {
color: #888;
font-size: x-small;
}
.meta {
color: #888;
font-size: x-small;
}
.meta a {
color: #369;
}
.actions a {
color: #888;
font-weight: bold;
padding: 0 4px;
}
.actions a:first-child {
padding-left:0;
}
.morelink {
display:block;
text-align:center;
position:relative;
-moz-border-radius-topleft:6px;
-moz-border-radius-bottomleft:6px;
border:1px solid #c4dbf1;
background:white none repeat-x scroll center left;
background-image:url('gradient-button.png');
font-size:150%;
font-weight:bold;
letter-spacing:-1px;
line-height:29px;
height:29px;
}
.morelink:hover, .mlh {
border-color:#879eb4;
background-image:url('gradient-button-hover.png');
}
.morelink a {
display:block;
width:100%;
color:#369;
}
.morelink:hover a {
color:white;
}
.morelink .nub {
position:absolute;
top:-1px;
right:-1px;
height:31px;
width:24px;
background:white none no-repeat scroll center left;
background-image:url('sprite.png');
background-position:-4px -4px;
}
.morelink:hover .nub, .mlhn {
background-image:url('sprite.png');
background-position:-4px -43px;
}
</style>
<script>
chrome.windows.getCurrent(function(currWindow) {
chrome.tabs.getSelected(currWindow.id, function(currTab) {
processApi(JSON.parse(window.localStorage.getItem(currTab.url)).response);
});
});
function processApi(response) {
document.getElementById('posts').setAttribute('data-modhash',response.data.modhash);
document.getElementById('posts').setAttribute('data-url',response.data.children[0].data.url);
var children = response.data.children;
var now = new Date();
for(var i = 0; i < children.length; i++) {
var data = children[i].data;
var entry = document.createElement('li');
entry.id = data.name;
if (data.likes === true) entry.setAttribute('data-dir','1');
if (data.likes === null) entry.setAttribute('data-dir','0');
if (data.likes === false) entry.setAttribute('data-dir','-1');
var votes = document.createElement('div');
votes.className = 'votes';
var upmod = document.createElement('a');
upmod.className = 'upmod';
upmod.addEventListener('click',upmodPost);
votes.appendChild(upmod);
var count = document.createElement('span');
count.className = 'count';
count.id = 'count_' + data.name;
count.innerHTML = data.score;
count.title = data.ups + ' up votes, ' + data.downs + ' down votes';
votes.appendChild(count);
var downmod = document.createElement('a');
downmod.className = 'downmod';
downmod.id = 'down_' + data.name;
votes.appendChild(downmod);
entry.appendChild(votes);
var thumblink = document.createElement('a');
thumblink.className = 'thumblink';
thumblink.href = 'http://www.reddit.com' + data.permalink;
thumblink.target = '_blank';
thumblink.title = 'View this submission on reddit';
var thumb = document.createElement('img');
thumb.className = 'thumb';
data.thumbnail.indexOf('/') === 0 ? thumb.src = 'http://www.reddit.com' + data.thumbnail : thumb.src = data.thumbnail;
thumb.alt = data.title;
thumblink.appendChild(thumb);
entry.appendChild(thumblink);
var post = document.createElement('div');
post.className = 'post';
var link = document.createElement('a');
link.className = 'link';
link.href = 'http://www.reddit.com' + data.permalink;
link.target = '_blank';
link.innerHTML = data.title;
link.title = 'View this submission on reddit';
post.appendChild(link);
var space = document.createTextNode(' ');
post.appendChild(space);
var domain = document.createElement('a');
domain.className = 'domain';
domain.href = 'http://www.reddit.com/domain/' + data.domain + '/';
domain.target = '_href';
domain.innerHTML = '(' + data.domain + ')';
post.appendChild(domain);
var meta = document.createElement('div');
meta.className = 'meta';
var timestamp = document.createElement('span');
timestamp.className = 'timestamp';
timestamp.innerHTML = 'submitted ' + prettyDate(ISODateString(new Date(data.created_utc * 1000)));
meta.appendChild(timestamp);
var by = document.createTextNode(' by ');
meta.appendChild(by);
var submitter = document.createElement('a');
submitter.className = 'submitter';
submitter.href = 'http://www.reddit.com/user/' + data.author + '/';
submitter.target = '_blank';
submitter.innerHTML = data.author;
meta.appendChild(submitter);
var to = document.createTextNode(' to ');
meta.appendChild(to);
var subreddit = document.createElement('a');
subreddit.className = 'subreddit';
subreddit.href = 'http://www.reddit.com/r/' + data.subreddit + '/';
subreddit.target = '_blank';
subreddit.innerHTML = data.subreddit;
meta.appendChild(subreddit);
post.appendChild(meta);
var actions = document.createElement('div');
actions.className = 'actions';
var comments = document.createElement('a');
comments.className = 'comments';
comments.href = 'http://www.reddit.com' + data.permalink;
comments.target = '_blank';
comments.innerHTML = data.num_comments + ' comments';
actions.appendChild(comments);
post.appendChild(actions);
var share = document.createElement('a');
share.className = 'share';
share.innerHTML = 'share';
actions.appendChild(share);
var save = document.createElement('a');
save.className = 'save';
save.innerHTML = data.saved === true ? 'saved' : 'save';
actions.appendChild(save);
var hide = document.createElement('a');
hide.className = 'hide';
if(data.hidden === true) {
entry.className += ' hidden';
hide.innerHTML = 'unhide'
} else {
hide.innerHTML = 'hide';
}
actions.appendChild(hide);
var report = document.createElement('a');
report.className = 'report';
report.innerHTML = 'report';
actions.appendChild(report);
entry.appendChild(post);
document.getElementById('posts').appendChild(entry);
}
document.getElementById('submit').href = 'http://www.reddit.com/submit?url=' + encodeURI(document.getElementById('posts').getAttribute('data-url'));
}
function upmodPost() {
var formData = new FormData();
formData.append('id',this.parentNode.parentNode.id);
formData.append('uh',document.getElementById('posts').getAttribute('data-modhash'));
var voteWas = this.parentNode.parentNode.getAttribute('data-dir');
var voteCount = document.getElementById('count_' + this.parentNode.parentNode.id)
if(voteWas === '1') formData.append('dir','0');
if(voteWas === '0') formData.append('dir','1');
if(voteWas === '-1') formData.append('dir','1');
var api = new XMLHttpRequest();
api.open('POST','http://www.reddit.com/api/vote',false);
api.send(formData);
if (api.statusText === 'OK') {
if(voteWas === '1') {
this.parentNode.parentNode.setAttribute('data-dir','0');
voteCount.innerHTML --;
}
if(voteWas === '0') {
this.parentNode.parentNode.setAttribute('data-dir','1');
voteCount.innerHTML ++;
}
if(voteWas === '-1') {
this.parentNode.parentNode.setAttribute('data-dir','1');
voteCount.innerHTML ++;
voteCount.innerHTML ++;
}
voteCount.title = 'Voted!';
window.localStorage.removeItem(document.getElementById('posts').getAttribute('data-url'));
// TODO:
// Since I'm removing the data from the cache after voting, I need to add a
// message passing function so that if the url isn't in the cache when the
// popup is invoked, background.html will request the data and cache it so
// the popup can read it.
} else {
console.error('Error voting.\n' + api.statusText);
console.warn(api);
return false;
}
}
/*
* JavaScript Pretty Date
* Thanks to Dean Landolt's comment on
* http://ejohn.org/blog/javascript-pretty-date/#postcomment
*/
// Takes an ISO time and returns a string representing how
// long ago the date represents.
function prettyDate(date_str){
var time = ('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," ");
var seconds = (new Date - new Date(time)) / 1000;
var token = 'ago', list_choice = 1;
if (seconds < 0) {
seconds = Math.abs(seconds);
token = 'from now';
list_choice = 2;
}
var i = 0, format;
while (format = time_formats[i++]) if (seconds < format[0]) {
if (typeof format[2] == 'string')
return format[list_choice];
else
return Math.floor(seconds / format[2]) + ' ' + format[1] + ' ' + token;
}
return time;
};
var time_formats = [
[60, 'just now', 1], // 60
[120, '1 minute ago', '1 minute from now'], // 60*2
[3600, 'minutes', 60], // 60*60, 60
[7200, '1 hour ago', '1 hour from now'], // 60*60*2
[86400, 'hours', 3600], // 60*60*24, 60*60
[172800, 'yesterday', 'tomorrow'], // 60*60*24*2
[604800, 'days', 86400], // 60*60*24*7, 60*60*24
[1209600, 'last week', 'next week'], // 60*60*24*7*4*2
[2419200, 'weeks', 604800], // 60*60*24*7*4, 60*60*24*7
[4838400, 'last month', 'next month'], // 60*60*24*7*4*2
[29030400, 'months', 2419200], // 60*60*24*7*4*12, 60*60*24*7*4
[58060800, 'last year', 'next year'], // 60*60*24*7*4*12*2
[2903040000, 'years', 29030400], // 60*60*24*7*4*12*100, 60*60*24*7*4*12
[5806080000, 'last century', 'next century'], // 60*60*24*7*4*12*100*2
[58060800000, 'centuries', 2903040000] // 60*60*24*7*4*12*100*20, 60*60*24*7*4*12*100
];
/*
* ISO 8601 Formatted Dates
* Gotten from the Mozilla Developer Center
*/
function ISODateString(d){
function pad(n){return n<10 ? '0'+n : n}
return d.getUTCFullYear()+'-'
+ pad(d.getUTCMonth()+1)+'-'
+ pad(d.getUTCDate())+'T'
+ pad(d.getUTCHours())+':'
+ pad(d.getUTCMinutes())+':'
+ pad(d.getUTCSeconds())+'Z'}
</script>
</head>
<body>
<ol id="posts">
</ol>
<div class="morelink">
<a id="submit" target="_blank">Submit this link again</a>
<div class="nub"></div>
</div>
</body>
</html>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

+30
View File
@@ -0,0 +1,30 @@
// UNSAVE
Request URL:http://www.reddit.com/api/unsave
Request Method:POST
Form Data:
id:t3_i0jf9
executed:unsaved
r:blog
uh:z59wa0u7v99260aa07baf45a198ac9434c5472f353f64dec22
renderstyle:html
// SAVE
Request URL:http://www.reddit.com/api/save
Request Method:POST
Form Data:
id:t3_i0jf9
executed:saved
r:blog
uh:1omqs0c192e8af3ad69b008c7de53875a6247774fc08173f07
renderstyle:html
// SEARCH REDDIT NAMES
Request URL:http://www.reddit.com/api/search_reddit_names
Request Method:POST
Form Data:
query:rp
uh:zvs6d0sz4he44b5174238157140615b04be2d0185a5a9b4879
renderstyle:html
// IDEAS
How about parsing imgur to look for direct AND imgur links?