1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
mask-detector/background.js

107 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2017-09-22 11:08:12 +02:00
function displayData(data, tabId, typo3) {
console.table(data);
2017-09-22 11:08:12 +02:00
var icon;
if (typo3) {
chrome.pageAction.show(tabId);
if (data.mask.found) {
icon = "icon";
} else if (data.tv.found) {
icon = "icon-tv";
} else if (typo3) {
icon = "icon-typo3";
} else {
icon = "icon-disabled";
2017-09-21 08:31:51 +02:00
}
2017-09-22 11:08:12 +02:00
} else {
chrome.pageAction.hide(tabId);
icon = "icon-disabled";
}
2017-09-21 08:31:51 +02:00
chrome.pageAction.setIcon({
tabId: tabId,
path: {
2017-09-22 11:08:12 +02:00
"128": "img/" + icon + ".128.png"
2017-09-21 08:31:51 +02:00
}
2017-09-22 11:19:34 +02:00
});
2017-09-21 08:31:51 +02:00
}
2017-09-22 11:08:12 +02:00
function handleMessage(request, sender) {
console.log("recieved message");
2017-09-20 15:33:18 +02:00
var tabId = sender.tab.id;
if (request.typo3) {
chrome.storage.local.get(request.url, function (result) {
var data = result[request.url];
2017-09-20 15:52:40 +02:00
if (!data || Date.now() - data.updated > 1000 * 60 * 60 * 24) {
2017-09-22 11:08:12 +02:00
data = {};
2017-09-20 15:33:18 +02:00
var xmlHttp = new XMLHttpRequest();
2017-09-22 11:08:12 +02:00
data.mask = {};
2017-09-20 15:33:18 +02:00
xmlHttp.onreadystatechange = function () {
2017-09-20 16:21:17 +02:00
if (xmlHttp.readyState === 4) {
2017-09-22 11:08:12 +02:00
data.mask.found = xmlHttp.status === 200;
data.mask.type = "new";
2017-09-22 11:08:12 +02:00
data.mask.modified = xmlHttp.getResponseHeader('Last-Modified');
var xml2Http = new XMLHttpRequest();
xml2Http.onreadystatechange = function () {
if (xml2Http.readyState === 4) {
if (xml2Http.status === 200) {
data.mask.found = true;
data.mask.type = "old";
data.mask.modified = xml2Http.getResponseHeader('Last-Modified');
}
var tvHttp = new XMLHttpRequest();
data.tv = {};
tvHttp.onreadystatechange = function () {
if (tvHttp.readyState === 4) {
data.tv.found = tvHttp.status === 200;
data.tv.modified = tvHttp.getResponseHeader('Last-Modified');
var cl = new XMLHttpRequest();
cl.onreadystatechange = function () {
if (cl.readyState === 4) {
if (cl.status === 200) {
var changelog = cl.responseText.split('\n', 1)[0];
var regex = /(\d\.)+(\d+)/;
var match = regex.exec(changelog);
data.t3version = match[0];
}
console.log("saving data");
data.updated = Date.now();
2017-09-22 11:08:12 +02:00
displayData(data, tabId, true);
2017-09-22 11:08:12 +02:00
var storage = {};
storage[request.url] = data;
chrome.storage.local.set(storage);
}
};
2017-09-22 11:08:12 +02:00
cl.open("GET", request.url + "/typo3_src/ChangeLog", true);
console.info("request changelog");
cl.send();
2017-09-22 11:08:12 +02:00
}
};
tvHttp.open("GET", request.url + "/typo3conf/ext/templavoila/ext_icon.gif", true);
console.info("request tv icon");
tvHttp.send();
2017-09-22 11:08:12 +02:00
}
};
xml2Http.open("GET", request.url + "/typo3conf/ext/mask/ext_icon.gif", true);
console.info("request old mask icon");
xml2Http.send();
2017-09-20 15:33:18 +02:00
}
};
xmlHttp.open("GET", request.url + "/typo3conf/ext/mask/Resources/Public/Icons/Extension.svg", true);
console.info("request new mask icon");
2017-09-20 15:33:18 +02:00
xmlHttp.send();
2017-09-22 11:08:12 +02:00
}
else {
2017-09-22 11:19:34 +02:00
displayData(data, tabId, true);
2017-09-20 15:33:18 +02:00
}
});
}
else {
2017-09-22 11:19:34 +02:00
displayData(false, tabId, false);
2017-09-20 15:33:18 +02:00
}
}
chrome.runtime.onMessage.addListener(handleMessage);