Use asynchronous requests for Instagram

This commit is contained in:
Les De Ridder 2016-05-05 14:06:37 +02:00
parent 8c3a4c3f15
commit c51209e2c3
1 changed files with 55 additions and 53 deletions

View File

@ -31,7 +31,7 @@ function copyToClipboard(url) {
chrome.pageAction.onClicked.addListener(function(tab) { chrome.pageAction.onClicked.addListener(function(tab) {
getCurrentTabUrl(function(url) { getCurrentTabUrl(function(url) {
var betterUrl = getBetterUrl(url); getBetterUrl(url, function(betterUrl) {
var filename = betterUrl.substr(betterUrl.lastIndexOf("/")); var filename = betterUrl.substr(betterUrl.lastIndexOf("/"));
filename = cleanExtension(filename); filename = cleanExtension(filename);
@ -82,8 +82,9 @@ chrome.pageAction.onClicked.addListener(function(tab) {
}); });
}); });
}); });
});
function getBetterUrl(url) { function getBetterUrl(url, callback) {
var extensionIndex = url.lastIndexOf('.'); var extensionIndex = url.lastIndexOf('.');
var extension = url.substr(extensionIndex + 1); var extension = url.substr(extensionIndex + 1);
@ -99,12 +100,10 @@ function getBetterUrl(url) {
url = url + ':orig'; url = url + ':orig';
} }
if(isInstagramUrl(url)) { if(isInstagramUrl(url)) {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('GET', 'https://api.instagram.com/oembed/?callback=&url=' + url, false); //TODO: Make this asynchronous somehow request.open('GET', 'https://api.instagram.com/oembed/?callback=&url=' + url, true);
request.send(null); request.onload = function () {
var json = JSON.parse(request.responseText); var json = JSON.parse(request.responseText);
var filename = json.thumbnail_url.substr(json.thumbnail_url.lastIndexOf('/') + 1); var filename = json.thumbnail_url.substr(json.thumbnail_url.lastIndexOf('/') + 1);
@ -112,10 +111,12 @@ function getBetterUrl(url) {
var baseurl = 'https://scontent-amt2-1.cdninstagram.com/'; var baseurl = 'https://scontent-amt2-1.cdninstagram.com/';
return baseurl + '/' + filename; callback(baseurl + '/' + filename);
};
request.send();
} else {
callback(url);
} }
return url;
} }
function cleanExtension(url) { function cleanExtension(url) {
@ -154,9 +155,10 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.onActivated.addListener(function(activeInfo) { chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) { chrome.tabs.get(activeInfo.tabId, function(tab) {
if(isCoolUrl(getBetterUrl(tab.url))) getBetterUrl(tab.url, function(betterUrl) {
{ if(isCoolUrl(betterUrl)) {
chrome.pageAction.show(activeInfo.tabId); chrome.pageAction.show(activeInfo.tabId);
} }
})
}); });
}); });