Permalink
Browse files

Small efficiency changes

  • Loading branch information...
1 parent 1434f50 commit 713af02b51e95b87b7c00fe39f8cbdd2a8e09ac7 @williamparry committed Jun 28, 2012
Showing with 39 additions and 62 deletions.
  1. +37 −54 src/background.js
  2. +2 −8 src/conduit.js
View
91 src/background.js
@@ -25,6 +25,13 @@ function updateBody(str) {
document.body.appendChild(documentFragment);
currentTitle = document.querySelectorAll('title')[0].innerHTML;
}
+
+function flashMessage(title, description) {
+ var notification = webkitNotifications.createNotification("img/logo.png", title, description);
+ notification.show();
+ setTimeout(function () { notification.cancel(); }, 3000);
+}
+
function getToken() {
if (!tokenScheduleIsRunning) {
tokenScheduleIsRunning = true;
@@ -127,44 +134,6 @@ function getBoards() {
}
}
-function flashLogout() {
- var notification = webkitNotifications.createNotification("img/logo.png", "You're logged out!", "Please log into Pinterest and try again.");
- notification.show();
- setTimeout(function () { notification.cancel(); }, 3000);
-}
-
-
-// Edit pin
-chrome.extension.onRequest.addListener(function (resp, sender, sendResponse) {
- setActive();
- var xhr = new XMLHttpRequest();
- xhr.open("POST", "https://pinterest.com/pin/" + resp.Data.pin_id + "/edit/", true);
- xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- updateBody(xhr.responseText);
- if(currentTitle === Titles.Login) {
- flashLogout();
- handleLoggedOut();
- sendResponse(false);
- } else {
- sendResponse(true);
- }
- } else {
- sendResponse(false);
- }
- setInactive();
- }
- }
-
- xhr.send("details=" + resp.Data.description + "&link=" + resp.Data.url + "&board=" + resp.Data.board_id + "&csrfmiddlewaretoken=" + token);
-
-
-
-
-});
-
function pin(board, media_url, title) {
setActive();
chrome.tabs.getSelected(null, function (tab) {
@@ -175,22 +144,16 @@ function pin(board, media_url, title) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
-
// Potential conflict with getBoards method, but not likely
updateBody(xhr.responseText);
if (currentTitle === Titles.Verification) {
chrome.tabs.create({url: "https://pinterest.com/verify_captcha/?src=pinmarklet&return=%2F"});
-
- var notification = webkitNotifications.createNotification("img/logo.png", "Verification needed", "Pinterest wants you to prove you are not a robot first...");
- notification.show();
- setTimeout(function () { notification.cancel(); }, 3000);
-
+ flashMessage("Verification needed", "Pinterest wants you to prove you are not a robot first...");
} else if(currentTitle === Titles.Login) {
- flashLogout();
+ flashMessage("You're logged out!", "Please log into Pinterest and try again.");
handleLoggedOut();
} else {
-
var data = {
tab_id: tab.id,
url: tab.url,
@@ -200,7 +163,6 @@ function pin(board, media_url, title) {
board_id: board,
board_name: document.querySelectorAll(".pinSuccess h3 a")[0].innerHTML
}
-
if (pinterestedTabs.indexOf(tab.id) !== -1) {
chrome.tabs.sendRequest(tab.id, data);
} else {
@@ -209,23 +171,44 @@ function pin(board, media_url, title) {
chrome.tabs.sendRequest(tab.id, data);
});
}
-
}
-
-
} else {
- var notification = webkitNotifications.createNotification("img/logo.png", "Something has gone wrong...", "Try again?");
- notification.show();
- setTimeout(function () { notification.cancel(); }, 3000);
+ flashMessage("Something has gone wrong...", "Try again?");
}
-
setInactive();
}
};
xhr.send("board=" + board + "&currency_holder=buyable&peeps_holder=replies&tag_holder=tags&title=" + title + "&media_url=" + encodeURIComponent(media_url) + "&url=" + encodeURIComponent(tab.url) + "&csrfmiddlewaretoken=" + token + "&caption=" + title);
});
}
+
+// Edit pin
+chrome.extension.onRequest.addListener(function (resp, sender, sendResponse) {
+ setActive();
+ var xhr = new XMLHttpRequest();
+ xhr.open("POST", "https://pinterest.com/pin/" + resp.Data.pin_id + "/edit/", true);
+ xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
+ xhr.onreadystatechange = function () {
+ if (xhr.readyState === 4) {
+ if (xhr.status === 200) {
+ updateBody(xhr.responseText);
+ if(currentTitle === Titles.Login) {
+ flashMessage("You're logged out!", "Please log into Pinterest and try again.");
+ handleLoggedOut();
+ sendResponse(false);
+ } else {
+ sendResponse(true);
+ }
+ } else {
+ sendResponse(false);
+ }
+ setInactive();
+ }
+ }
+ xhr.send("details=" + resp.Data.description + "&link=" + resp.Data.url + "&board=" + resp.Data.board_id + "&csrfmiddlewaretoken=" + token);
+});
+
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({ "url": "https://pinterest.com/", "selected": true });
});
View
10 src/conduit.js
@@ -1,6 +1,5 @@
var NotificationHeight = 100;
-// http://www.visual-experiments.com/2011/12/05/how-to-bypass-webgl-sop-restriction-v2/
function arrayBufferDataUri(raw){var base64='';var encodings='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var bytes=new Uint8Array(raw);var byteLength=bytes.byteLength;var byteRemainder=byteLength%3;var mainLength=byteLength-byteRemainder;var a,b,c,d;var chunk;for(var i=0;i<mainLength;i=i+3){chunk=(bytes[i]<<16)|(bytes[i+1]<<8)|bytes[i+2];a=(chunk&16515072)>>18;b=(chunk&258048)>>12;c=(chunk&4032)>>6;d=chunk&63;base64+=encodings[a]+encodings[b]+encodings[c]+encodings[d]}
if(byteRemainder==1){chunk=bytes[mainLength];a=(chunk&252)>>2;b=(chunk&3)<<4;base64+=encodings[a]+encodings[b]+'==';}
else if(byteRemainder==2){chunk=(bytes[mainLength]<<8)|bytes[mainLength+1];a=(chunk&16128)>>8;b=(chunk&1008)>>4;c=(chunk&15)<<2;base64+=encodings[a]+encodings[b]+encodings[c]+'=';}
@@ -28,24 +27,19 @@ function notify(data) {
xhr.responseType = "arraybuffer";
xhr.onload = function() {
var img = new Image();
-
data.imgData = arrayBufferDataUri(xhr.response);
-
iFrame.src = chrome.extension.getURL("notification.html?r=" + Math.round(Math.random() * 999999) + "#data=" + JSON.stringify(data));
-
window.addEventListener("message", function (evt) {
if (evt.data == data.pin_id) {
document.documentElement.removeChild(iFrame);
window.removeEventListener("message", arguments.callee);
}
}, false);
-
document.documentElement.appendChild(iFrame);
-
};
xhr.send(null);
-
}
-
+// First the background page checks to see if the script has already been added to the page
+// Then it sends a request (there's only one), which is handled by notify()
chrome.extension.onRequest.addListener(notify);

0 comments on commit 713af02

Please sign in to comment.