Use asynchronous requests for Instagram
This commit is contained in:
parent
8c3a4c3f15
commit
c51209e2c3
108
background.js
108
background.js
|
@ -31,59 +31,60 @@ 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);
|
||||||
|
|
||||||
chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false, disableurlblacklist: false}, function(config) {
|
chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false, disableurlblacklist: false}, function(config) {
|
||||||
if(config.url == '') {
|
if(config.url == '') {
|
||||||
alert("Please select a Pomf clone.");
|
alert("Please select a Pomf clone.");
|
||||||
chrome.tabs.create({ url: "options.html" });
|
chrome.tabs.create({ url: "options.html" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var blackList = ["pomf.", "mixtape.moe", "catgirlsare.sexy", "cocaine.ninja"];
|
var blackList = ["pomf.", "mixtape.moe", "catgirlsare.sexy", "cocaine.ninja"];
|
||||||
|
|
||||||
if(!config.disableblacklist) {
|
if(!config.disableblacklist) {
|
||||||
for(black of blackList) {
|
for(black of blackList) {
|
||||||
if(url.indexOf(black) != -1) {
|
if(url.indexOf(black) != -1) {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var worker = new Worker('worker.js');
|
var worker = new Worker('worker.js');
|
||||||
worker.onmessage = function(event) {
|
worker.onmessage = function(event) {
|
||||||
var response = JSON.parse(event.data);
|
var response = JSON.parse(event.data);
|
||||||
|
|
||||||
var newUrl = response.files[0].url;
|
var newUrl = response.files[0].url;
|
||||||
|
|
||||||
switch(config.tabbehaviour)
|
switch(config.tabbehaviour)
|
||||||
{
|
{
|
||||||
case "newtab":
|
case "newtab":
|
||||||
chrome.tabs.create({url: newUrl});
|
chrome.tabs.create({url: newUrl});
|
||||||
break;
|
break;
|
||||||
case "replacetab":
|
case "replacetab":
|
||||||
chrome.tabs.update(tab.id, {url: newUrl});
|
chrome.tabs.update(tab.id, {url: newUrl});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(config.copytoclipboard) {
|
if(config.copytoclipboard) {
|
||||||
copyToClipboard(newUrl);
|
copyToClipboard(newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(config.replacebookmark) {
|
if(config.replacebookmark) {
|
||||||
replaceBookmark(url, newUrl);
|
replaceBookmark(url, newUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
worker.terminate();
|
worker.terminate();
|
||||||
};
|
};
|
||||||
worker.postMessage(JSON.stringify({pomfclone: config.url, url: betterUrl, filename: filename}));
|
worker.postMessage(JSON.stringify({pomfclone: config.url, url: betterUrl, filename: filename}));
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
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,23 +100,23 @@ 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);
|
||||||
|
filename = filename.substr(0, filename.indexOf('?'));
|
||||||
|
|
||||||
var filename = json.thumbnail_url.substr(json.thumbnail_url.lastIndexOf('/') + 1);
|
var baseurl = 'https://scontent-amt2-1.cdninstagram.com/';
|
||||||
filename = filename.substr(0, filename.indexOf('?'));
|
|
||||||
|
|
||||||
var baseurl = 'https://scontent-amt2-1.cdninstagram.com/';
|
callback(baseurl + '/' + filename);
|
||||||
|
};
|
||||||
return 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);
|
||||||
}
|
}
|
||||||
|
})
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue