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,59 +31,60 @@ function copyToClipboard(url) {
chrome.pageAction.onClicked.addListener(function(tab) {
getCurrentTabUrl(function(url) {
var betterUrl = getBetterUrl(url);
var filename = betterUrl.substr(betterUrl.lastIndexOf("/"));
filename = cleanExtension(filename);
getBetterUrl(url, function(betterUrl) {
var filename = betterUrl.substr(betterUrl.lastIndexOf("/"));
filename = cleanExtension(filename);
chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false, disableurlblacklist: false}, function(config) {
if(config.url == '') {
alert("Please select a Pomf clone.");
chrome.tabs.create({ url: "options.html" });
return;
}
chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false, disableurlblacklist: false}, function(config) {
if(config.url == '') {
alert("Please select a Pomf clone.");
chrome.tabs.create({ url: "options.html" });
return;
}
var blackList = ["pomf.", "mixtape.moe", "catgirlsare.sexy", "cocaine.ninja"];
var blackList = ["pomf.", "mixtape.moe", "catgirlsare.sexy", "cocaine.ninja"];
if(!config.disableblacklist) {
for(black of blackList) {
if(url.indexOf(black) != -1) {
return;
if(!config.disableblacklist) {
for(black of blackList) {
if(url.indexOf(black) != -1) {
return;
}
}
}
}
var worker = new Worker('worker.js');
worker.onmessage = function(event) {
var response = JSON.parse(event.data);
var worker = new Worker('worker.js');
worker.onmessage = function(event) {
var response = JSON.parse(event.data);
var newUrl = response.files[0].url;
var newUrl = response.files[0].url;
switch(config.tabbehaviour)
{
case "newtab":
chrome.tabs.create({url: newUrl});
break;
case "replacetab":
chrome.tabs.update(tab.id, {url: newUrl});
break;
}
switch(config.tabbehaviour)
{
case "newtab":
chrome.tabs.create({url: newUrl});
break;
case "replacetab":
chrome.tabs.update(tab.id, {url: newUrl});
break;
}
if(config.copytoclipboard) {
copyToClipboard(newUrl);
}
if(config.copytoclipboard) {
copyToClipboard(newUrl);
}
if(config.replacebookmark) {
replaceBookmark(url, newUrl);
}
if(config.replacebookmark) {
replaceBookmark(url, newUrl);
}
worker.terminate();
};
worker.postMessage(JSON.stringify({pomfclone: config.url, url: betterUrl, filename: filename}));
worker.terminate();
};
worker.postMessage(JSON.stringify({pomfclone: config.url, url: betterUrl, filename: filename}));
});
});
});
});
function getBetterUrl(url) {
function getBetterUrl(url, callback) {
var extensionIndex = url.lastIndexOf('.');
var extension = url.substr(extensionIndex + 1);
@ -99,23 +100,23 @@ function getBetterUrl(url) {
url = url + ':orig';
}
if(isInstagramUrl(url)) {
var request = new XMLHttpRequest();
request.open('GET', 'https://api.instagram.com/oembed/?callback=&url=' + url, false); //TODO: Make this asynchronous somehow
request.send(null);
request.open('GET', 'https://api.instagram.com/oembed/?callback=&url=' + url, true);
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);
filename = filename.substr(0, filename.indexOf('?'));
var filename = json.thumbnail_url.substr(json.thumbnail_url.lastIndexOf('/') + 1);
filename = filename.substr(0, filename.indexOf('?'));
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) {
@ -154,9 +155,10 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.onActivated.addListener(function(activeInfo) {
chrome.tabs.get(activeInfo.tabId, function(tab) {
if(isCoolUrl(getBetterUrl(tab.url)))
{
chrome.pageAction.show(activeInfo.tabId);
}
getBetterUrl(tab.url, function(betterUrl) {
if(isCoolUrl(betterUrl)) {
chrome.pageAction.show(activeInfo.tabId);
}
})
});
});