Browse files

updated to observers and #newtwitter

  • Loading branch information...
1 parent c0e3e98 commit 58b572d06d7167d6ea8b84c636edf61fef1ef0f2 @abraham committed May 22, 2012
Showing with 67 additions and 58 deletions.
  1. +19 −16 twitter-creation-date/background.js
  2. +33 −33 twitter-creation-date/content.js
  3. +15 −9 twitter-creation-date/manifest.json
View
35 twitter-creation-date/background.js
@@ -1,19 +1,22 @@
-<script>
-chrome.extension.onRequest.addListener(
- function(request, sender, sendResponse) {
- var xhr = new XMLHttpRequest();
- xhr.open("GET", "https://api.twitter.com/1/users/show/"+ request.screen_name + ".json", true);
- xhr.send();
- xhr.onreadystatechange = function() {
- if (xhr.readyState == 4) {
- if (xhr.status == 200) {
- sendResponse(JSON.parse(xhr.responseText));
- } else {
- sendResponse({});
- }
- }
+chrome.extension.onRequest.addListener(handleUserId);
+
+function handleUserId(request, sender, sendResponse) {
+ // TODO: check for cached data
+ var xhr = new XMLHttpRequest();
+ xhr.open("GET", "https://api.twitter.com/1/users/show.json?user_id=" + request.userId, true);
+ xhr.send();
+ xhr.onreadystatechange = function() {
+ if (xhr.readyState == 4) {
+ // TODO: cache success data
+ sendResponse({ status: xhr.status, response: JSON.parse(xhr.responseText) });
}
}
-);
+}
-</script>
+if (localStorage.getItem('installed') !== 'true') {
+ var url = chrome.extension.getURL('cracked-twitter.png');
+ var body = 'The extension Twitter Creation Date is now Born for Twitter and has been updated to work with #newtwitter.';
+ var popup = window.webkitNotifications.createNotification(url, 'Twitter Creation Date Reborn', body);
+ popup.show();
+ localStorage.setItem('installed', 'true');
+}
View
66 twitter-creation-date/content.js
@@ -1,37 +1,37 @@
-// Update created_at value on profile page.
-function creation_updatePage(response) {
- if(response.created_at) {
- var created_at = new Date(response.created_at);
- $('#creation_stat').text(created_at.toDateString());
- }
-}
-
-// Identify and validate profile and initiate API call.
-var page = $('meta[name=page-user-screen_name]')[0];
-var primary = $('#primary_nav');
-if(page && primary) {
- insertLi('http://abrah.am/', '???');
- chrome.extension.sendRequest({screen_name: page.content}, creation_updatePage);
-}
+// TODO: adds support for modals
+var observer = new MutationSummary({
+ callback: handleProfileChange,
+ queries: [{ element: 'div.profile-card-inner' }]
+});
-// Insert <li> into profile page
-function insertLi(link, text) {
- var stat = $('<span>' + text + '</span>');
- stat.addClass('stat_count');
- stat.attr('id', 'creation_stat');
+function handleProfileChange(summaries) {
+ // TODO: loop through summaries?
+ var summary = summaries[0];
- var title = $('<span>Created</span>');
-
- var anchor = $('<a href="' + link + '"></a>');
- anchor.attr('target', '_blank');
- anchor.attr('id', 'creation_anchor');
- anchor.attr('title', 'Twitter Creation extension built by Abraham Williams (@abraham)');
- anchor.append(stat).append(title);
-
- var tab = $('<li></li>');
- tab.attr('id', 'creation_tab');
- tab.append(anchor);
+ summary.added.forEach(function(profile) {
+ var userId = profile.getAttribute('data-user-id');
+ profile.className += ' born-profile-' + userId;
+ chrome.extension.sendRequest(null, { userId: userId }, handleResponse);
+ });
+}
- var nav = $('#primary_nav');
- nav.append(tab);
+function handleResponse(response) {
+ if (response.status === 200) {
+ // TODO: Clean up code
+ var d = document.createElement('span');
+ d.className = 'divider';
+ d.innerText = '·';
+ var profile = response.response;
+ var profilesOnPage = document.getElementsByClassName('born-profile-' + profile.id);
+ var s = document.createElement('span');
+ var createdAt = new Date(profile.created_at);
+ s.innerText = 'Born ' + createdAt.toDateString();
+ s.setAttribute('title', 'Born created by @abraham');
+ var lnu = profilesOnPage[0].getElementsByClassName('location-and-url');
+ lnu[0].appendChild(d).parentNode.appendChild(s);
+ } else {
+ // TODO: display rate limit errors
+ // console.log('Error response');
+ // console.log(response);
+ }
}
View
24 twitter-creation-date/manifest.json
@@ -1,22 +1,28 @@
{
- "name": "Twitter Creation Date",
+ "name": "Born for Twitter",
"background": { "scripts": ["background.js"] },
"content_scripts": [ {
- "all_frames": true,
- "js": [ "jquery-1.4.2.min.js", "content_script.js" ],
+ "js": [ "mutation_summary.js", "content.js" ],
"matches": [
"http://twitter.com/*",
"https://twitter.com/*"
],
"run_at": "document_end"
} ],
- "description": "Displays the creation date of Twitter accounts on their profiles.",
- "permissions": [ "https://api.twitter.com/*" ],
+ "description": "Display when a profile was born on twitter.com. Previously known as Twitter Creation Date.",
+ "permissions": [
+ "https://api.twitter.com/*",
+ "notifications"
+ ],
"version": "3",
"icons": {
- "48": "poseurtech.png",
- "128": "poseurtech.png"
+ "16": "cracked-twitter.png",
+ "48": "cracked-twitter.png",
+ "128": "cracked-twitter.png"
},
- "manifest_version": 2,
- "minimum_chrome_version": "18"
+ "manifest_version": "2012.5.22",
+ "minimum_chrome_version": "18",
+ "web_accessible_resources": [
+ "cracked-twitter.png"
+ ]
}

0 comments on commit 58b572d

Please sign in to comment.