255 lines
6.6 KiB
JavaScript
255 lines
6.6 KiB
JavaScript
function getCurrentTabUrl(callback) {
|
|
var queryInfo = {
|
|
active: true,
|
|
currentWindow: true
|
|
};
|
|
|
|
chrome.tabs.query(queryInfo, function(tabs) {
|
|
var tab = tabs[0];
|
|
|
|
var url = tab.url;
|
|
|
|
callback(url);
|
|
});
|
|
}
|
|
|
|
function replaceBookmark(oldUrl, newUrl) {
|
|
chrome.bookmarks.search({url: oldUrl}, function(bookmarks) {
|
|
for(bookmark of bookmarks) {
|
|
chrome.bookmarks.update(bookmark.id, {url: newUrl});
|
|
}
|
|
});
|
|
}
|
|
|
|
function copyToClipboard(url) {
|
|
document.oncopy = function(event) {
|
|
event.clipboardData.setData("text/plain", url);
|
|
event.preventDefault();
|
|
};
|
|
document.execCommand('Copy', false, null);
|
|
}
|
|
|
|
var blacklist = ["pomf.", "mixtape.moe", "catgirlsare.sexy", "p.fuwafuwa.moe", "/wiki/", "catbox.moe", "fuwa.se"];
|
|
|
|
chrome.pageAction.onClicked.addListener(function(tab) {
|
|
getCurrentTabUrl(function(url) {
|
|
getBetterUrl(url, function(betterUrl) {
|
|
var filename = betterUrl.substr(betterUrl.lastIndexOf("/"));
|
|
filename = cleanExtension(filename);
|
|
|
|
if(filename.indexOf('.') == -1) {
|
|
filename = filename + '.jpg'; //on average, it's probably JPEG if it doesn't have an extension
|
|
}
|
|
|
|
chrome.storage.sync.get({url: '', tabbehaviour: '', copytoclipboard: false, replacebookmark: false, disableblacklist: false}, function(config) {
|
|
if(config.url == '') {
|
|
alert("Please select a Pomf clone.");
|
|
chrome.tabs.create({ url: "options.html" });
|
|
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 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;
|
|
}
|
|
|
|
if(config.copytoclipboard) {
|
|
copyToClipboard(newUrl);
|
|
}
|
|
|
|
if(config.replacebookmark) {
|
|
replaceBookmark(url, newUrl);
|
|
}
|
|
|
|
worker.terminate();
|
|
};
|
|
worker.postMessage(JSON.stringify({pomfclone: config.url, url: betterUrl, filename: filename}));
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
function getBetterUrl(url, callback) {
|
|
var extensionIndex = url.lastIndexOf('.');
|
|
var extension = url.substr(extensionIndex + 1);
|
|
|
|
if(extension == "gifv") {
|
|
url = url.replace("gifv", "mp4");
|
|
}
|
|
|
|
if(isTwitterUrl(url)) {
|
|
if(url.indexOf(':') != url.lastIndexOf(':')) { //Match if there are occurences of ':' other than the ':' in 'https://', such as in '.jpg:large'
|
|
url = url.substr(0, url.lastIndexOf(':'));
|
|
}
|
|
|
|
url = url + ':orig';
|
|
|
|
callback(url);
|
|
} else if(isInstagramUrl(url)) {
|
|
var request = new XMLHttpRequest();
|
|
request.open('GET', 'https://api.instagram.com/oembed/?callback=&url=' + url, true);
|
|
request.onload = function () {
|
|
var json = JSON.parse(request.responseText);
|
|
|
|
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/';
|
|
|
|
callback(baseurl + '/' + filename);
|
|
};
|
|
request.send();
|
|
} else if(isPixivUrl(url)) {
|
|
//TODO: Use a content script instead of re-fetching the page
|
|
|
|
var pageRequest = new XMLHttpRequest();
|
|
pageRequest.open('GET', url, true);
|
|
pageRequest.onload = function () {
|
|
var regex = /<div class="_layout-thumbnail ui-modal-trigger"><img src="[^"]+img-master\/([^"]+)_master[0-9]+(\.[^"]+)".*>/g;
|
|
|
|
var match = regex.exec(pageRequest.responseText);
|
|
|
|
var imageUrl = 'http://i3.pixiv.net/img-original/' + match[1];
|
|
|
|
var onload = function (imageRequest, extension) {
|
|
if(imageRequest.status == 404 && extension == "jpg") {
|
|
makerequest("png");
|
|
} else {
|
|
callback(imageUrl + "." + extension);
|
|
}
|
|
};
|
|
|
|
var makerequest = function(extension) {
|
|
var imageRequest = new XMLHttpRequest();
|
|
imageRequest.open('GET', imageUrl + "." + extension, true);
|
|
imageRequest.setRequestHeader('X-Pixiv-Referer', url);
|
|
imageRequest.onload = function() { onload(imageRequest, extension); };
|
|
imageRequest.send();
|
|
};
|
|
|
|
makerequest("jpg");
|
|
};
|
|
pageRequest.send();
|
|
} else if(isSkypeUrl(url)) {
|
|
url = url.substr(0, url.lastIndexOf('/'));
|
|
url = url + '/imgpsh_fullsize';
|
|
|
|
callback(url);
|
|
} else {
|
|
callback(url);
|
|
}
|
|
}
|
|
|
|
chrome.webRequest.onBeforeSendHeaders.addListener(
|
|
function(details) {
|
|
if(details.url.indexOf('pixiv') != -1) {
|
|
for (var i = 0; i < details.requestHeaders.length; ++i) {
|
|
if (details.requestHeaders[i].name === 'X-Pixiv-Referer') {
|
|
var referer = details.requestHeaders[i].value;
|
|
details.requestHeaders.splice(i, 1);
|
|
break;
|
|
}
|
|
}
|
|
|
|
details.requestHeaders.push({
|
|
name: 'Referer',
|
|
value: referer
|
|
});
|
|
}
|
|
|
|
return {requestHeaders: details.requestHeaders};
|
|
},
|
|
{urls: ["*://*/*"], types: ["xmlhttprequest"]},
|
|
["blocking", "requestHeaders"]
|
|
);
|
|
|
|
function cleanExtension(url) {
|
|
var trailingStrings = ["?", ":orig", ":large", ":small", ":thumb", ":medium"];
|
|
|
|
for(trailingString of trailingStrings) {
|
|
if(url.indexOf(trailingString) != -1) {
|
|
url = url.substr(0, url.indexOf(trailingString));
|
|
}
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
function isTwitterUrl(url) {
|
|
return url.indexOf('pbs.twimg.com/media/') != -1;
|
|
}
|
|
|
|
function isInstagramUrl(url) {
|
|
return url.indexOf('instagram.com/p') != -1;
|
|
}
|
|
|
|
function isPixivUrl(url) {
|
|
return url.indexOf('pixiv.net/member_illust.php') != -1;
|
|
}
|
|
|
|
function isSkypeUrl(url) {
|
|
return url.indexOf('api.asm.skype.com/v1/objects/') != -1;
|
|
}
|
|
|
|
function isCoolUrl(url, callback) {
|
|
url = url.toLowerCase();
|
|
|
|
var extensionIndex = url.lastIndexOf('.');
|
|
var extension = cleanExtension(url.substr(extensionIndex + 1));
|
|
|
|
var validExtensions = ["jpg", "jpeg", "png", "gif", "webm", "gifv", "mp4", "mp3", "ogg", "opus", "pdf", "mkv", "ogv", "swf", "flv"];
|
|
|
|
chrome.storage.sync.get({ disableblacklist: false }, function(config) {
|
|
if(!config.disableblacklist) {
|
|
for(black of blacklist) {
|
|
if(url.indexOf(black) != -1) {
|
|
return false;
|
|
}
|
|
}
|
|
} else {
|
|
console.log('blacklist disabled!');
|
|
}
|
|
|
|
callback(validExtensions.indexOf(extension) != -1 || isInstagramUrl(url) || isPixivUrl(url) || isSkypeUrl(url));
|
|
});
|
|
}
|
|
|
|
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
|
if(tab.active) {
|
|
isCoolUrl(tab.url, function (cool) {
|
|
if(cool) {
|
|
chrome.pageAction.show(tabId);
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
chrome.tabs.onActivated.addListener(function(activeInfo) {
|
|
chrome.tabs.get(activeInfo.tabId, function(tab) {
|
|
isCoolUrl(tab.url, function(cool) {
|
|
if (cool) {
|
|
chrome.pageAction.show(activeInfo.tabId);
|
|
}
|
|
});
|
|
});
|
|
});
|