Skip to content

Commit

Permalink
moved dev/ to stable/
Browse files Browse the repository at this point in the history
  • Loading branch information
drusepth committed Jan 28, 2011
1 parent a7c7519 commit 23840bb
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 80 deletions.
49 changes: 22 additions & 27 deletions stable/background.html
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
<html>
<head>
<script type="text/javascript">
// Config stuff -- perhaps allow users to change these later
var delay_seconds = 60; // How many seconds to wait between checking notifications
// Configuration
var delay_seconds = 60; // Seconds between requesting updates from Quora

// This is Chrome so we know XMLHttpRequest() works. None of that bullshit try/catch init
// AJAX Object
var ajax_transport = new XMLHttpRequest();

// Start
// Start asynch updating
update();

// Event listeners
// *clicks on extension icon*
chrome.browserAction.onClicked.addListener(function(tab)
{
alert("Sup");
});

// Functions
function update()
{
// sending request
ajax_transport.open("GET", "http://www.quora.com", true);
ajax_transport.onreadystatechange = process;
ajax_transport.send(null)
ajax_transport.open("GET", "http://api.quora.com/api/logged_in_user?fields=notifs,inbox", true);
ajax_transport.onreadystatechange = process;
ajax_transport.send(null)

// Do it again later
setTimeout(update, 1000 * delay_seconds);
Expand All @@ -33,23 +26,25 @@
{
if (ajax_transport.readyState != 4)
{
return;
return;
}

// scrrraaape
var re_n = new RegExp("<strong>Home</strong><span id=\".+\"><strong class=\"count\">(.+)</strong></span>", "i");
var content = ajax_transport.responseText;
var n = re_n.exec(content);
if (n == null) // regex didn't match
{
// Remove extension badge
chrome.browserAction.setBadgeText({text: ""});
}
else
// Strip while(1);
var response = ajax_transport.responseText;
response = response.substring("while(1);".length);

// Parse json
var json = JSON.parse(response);
var n_count = json.notifs.unseen_count;
var m_count = json.inbox.unread_count;

// Update badge count
var badgeText = (n_count > 0) ? String(n_count) : "";
if (m_count > 0 && String(n_count).length < 3)
{
// Set new badge
chrome.browserAction.setBadgeText({text: n[1]});
badgeText += ("/" + m_count);
}
chrome.browserAction.setBadgeText({text: badgeText});
}
</script>
</head>
Expand Down
14 changes: 8 additions & 6 deletions stable/manifest.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"name": "Quora",
"version": "1.0",
"version": "1.2.5",
"description": "See your notifications on Quora",
"background_page": "background.html",
"browser_action": {
"browser_action":
{
"default_icon": "favicon.ico",
"popup": "popup.html"
},
"icons": {
"icons":
{
"16": "favicon.ico",
"48": "48.png",
"128": "128.png"
},
"permissions": [
"tabs",
"http://www.quora.com/"
"permissions":
[
"http://api.quora.com/"
]
}
111 changes: 72 additions & 39 deletions stable/popup.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
var ajax_transport = ajax_init();
var ajax_transport = new XMLHttpRequest();
update_notifications();

function search()
Expand All @@ -10,12 +12,6 @@
chrome.tabs.create({url: "http://www.quora.com/search?q=" + q + "&context_type=&context_id="});
}

function ajax_init()
{
// Chrome so we knew XMLHttpRequest works -- joy!
return new XMLHttpRequest();
}

function update_notifications()
{
get_notifications();
Expand All @@ -25,57 +21,90 @@
function get_notifications()
{
// sending request
ajax_transport.open("GET", "http://www.quora.com", true);
ajax_transport.onreadystatechange = process_notifications;
ajax_transport.send(null)
ajax_transport.open("GET", "http://api.quora.com/api/logged_in_user?fields=notifs,inbox", true);
ajax_transport.onreadystatechange = process_notifications;
ajax_transport.send(null); // Causes the error
}

function process_notifications()
{
if (ajax_transport.readyState != 4)
{
return;
return;
}

// scrrraaape
var re_n = new RegExp("<div class=\"row unseen\"><div class=\"aggregated_item\"><div>.+ <a class=\"timestamp\" href=\"/log/revision/[^\"]+\">[^<]+</a></div></div>", "gmi");
var re_timestamp = new RegExp("<a class=\"timestamp\" href=\"/log/revision/[^\"]+\">[^<]+</a>", "gmi");
var re_quora_rw = new RegExp("href=\"/", "gmi");
var re_target_rw = new RegExp("<a ", "gmi");
// Strip while(1);
var response = ajax_transport.responseText;
response = response.substring("while(1);".length);
// Parse json
var json = JSON.parse(response);

var content = ajax_transport.responseText;
var notis;
var name = json.name;
var n_count = json.notifs.unseen_count;
var m_count = json.inbox.unread_count;
var t_notifs = json.notifs.unseen_aggregated_count;
var notifs = json.notifs.unseen;

if (re_n.test(content))
if (name == "undefined" || n_count == "undefined" || t_notifs == "undefined" || notifs == "undefined")
{
// Match & Modify
notis = String(content.match(re_n));
// Remove timestamps
notis = notis.replace(re_timestamp, "");
// Rewrite URLs to point at Quora
notis = notis.replace(re_quora_rw, "href=\"http://www.quora.com/");
// Make them open in new windows when clicked
notis = notis.replace(re_target_rw, "<a target=\"_quora_\" ");

// Create element
var notif = document.createElement('li');
notif.setAttribute('id', 'noti');

// Add element
document.getElementById('n_list').appendChild(notif);
document.getElementById('noti').innerHTML = notis;
// Error reading from api
return;
}

// Notifications
if (n_count > 0)
{
var notif_container = document.getElementById('n_list');

// Add all notifications to popup
for (var i = 0; i < (n_count > 5 ? 5 : n_count); i++)
{
var n = document.createElement('li');
n.setAttribute('id', 'notif_' + i);
n.setAttribute('class', 'notification');

notif_container.appendChild(n);

// Reformat as needed
notifs[i] = notifs[i].replace(/<a href=/gi, "<a target=\"_quora\" href=");
notifs[i] = notifs[i].replace(/<a target="_quora" href="#">.*?<\/a>/gi, "");
notifs[i] = notifs[i].replace(/\. They need topics to get started: /, "");

document.getElementById('notif_' + i).innerHTML = notifs[i];
}
}
else
{
// Hide empty notification box
document.getElementById('notifications').style.display = "none";
// Hide notifications container
document.getElementById('notifications').style.display = 'none';
}

// Mail
if (m_count > 0)
{
var container = document.getElementById('mail');

// Perhaps show inbox or feed here later?
container.innerHTML = 'You have <strong>' + m_count + '</strong> unread private message'
+ (m_count == 1 ? '.' : 's.') + '<br />'
+ '<a href="http://www.quora.com/inbox" target="_quora">Click here to '
+ 'visit your inbox.';
}
else
{
document.getElementById('mail').style.display = 'none';
}

// Update badge count
var badgeText = (n_count > 0) ? String(n_count) : "";
if (m_count > 0 && String(n_count).length < 3)
{
badgeText += ("/" + m_count);
}
chrome.browserAction.setBadgeText({text: badgeText});
}

</script>
</head>
<body>

<div id="header">
<a id="logo" href="http://www.quora.com" target="_quora">Quora</a>
Expand All @@ -88,10 +117,14 @@
</form>
</div>

<div id="mail"></div>

<div id="notifications">
<h3 id="n_title">New Notifications</h3>

<ul id="n_list">

</ul>
</div>
</body>
</html>
16 changes: 8 additions & 8 deletions stable/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ body
{
text-decoration: underline;
}
#notifications
.notification
{
border-top: 1px solid #C2D7EA;
padding-top: 5px;
padding-bottom: 5px;
}
#notifications, #mail
{
background-color: #DFEAF4;
border: 3px solid #BED4E8;
Expand All @@ -97,10 +103,4 @@ body
ul, ul li
{
list-style: none;
}
.aggregated_item
{
border-top: 1px solid #BED4E8;
padding-top: 3px;
margin-top: 3px;
}
}

0 comments on commit 23840bb

Please sign in to comment.