Commit a25f3618 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Transform into a web extension

parent 37b7ad078b2a
var NO_DATA = {
badge: "",
syntax: "",
fetch: function () {
return new Promise(function (resolve, reject) {
reject("No data!");
});
}
};
var allTabs = {};
function getTabData(id) {
if (!allTabs.hasOwnProperty(id)) {
allTabs[id] = {
isActive: false,
hasData: ""
linkedData: NO_DATA
};
}
return allTabs[id];
......@@ -25,7 +35,7 @@ function updateIcon(tabId) {
tabId: tabId
});
chrome.browserAction.setBadgeText({
text: data.hasData,
text: data.linkedData.badge,
tabId: tabId
});
}
......@@ -48,9 +58,102 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) { onTabActiv
chrome.tabs.onActivated.addListener(function (activeInfo) { onTabActivated(activeInfo.tabId); });
function fetchAt(uri) {
return new Promise(function (resolve, reject) {
reject("No data!");
});
}
function fetchContent(tabId) {
return new Promise(function (resolve, reject) {
reject("No data!");
});
}
function parseLinkTags(content) {
var tags = [];
var regexp = RegExp("([a-zA-Z_0-9]+)\\s*=\\s*('[^']*'|\"[^\"]*\")", "g");
var match;
while ((match = regexp.exec(content)) !== null) {
tags.push({
name: match[1],
value: match[2].substring(1, match[2].length - 1)
});
}
return tags;
}
function parseLinks(content) {
var links = [];
var regexp = RegExp("<([^>]*)>(?:\\s*;\\s*([a-zA-Z_0-9]+)\\s*=\\s*('[^']*'|\"[^\"]*\"))*", "g");
var match;
while ((match = regexp.exec(content)) !== null) {
var link = { url: match[1] };
var tags = parseLinkTags(match[0]);
for (var i = 0; i != tags.length; i++) {
link[tags[i].name] = tags[i].value;
}
links.push(link);
}
return links;
}
function getHeader(headers, name) {
for (var i = 0; i != headers.length; i++) {
if (headers[i].name == name) {
return headers[i].value;
}
}
return null;
}
function detectDataOnLinks(headers) {
linksContent = getHeader(headers, "Link");
if (linksContent == null)
return NO_DATA;
var links = parseLinks(linksContent);
for (var j = 0; j != links.length; j++) {
if (links[j].rel == "alternate" && links[j].type == "text/n3") {
return {
badge: "N3",
syntax: links[j].type,
fetch: function (url = links[j].url) {
return fetchAt(url);
}
};
}
}
return NO_DATA;
}
function detectDataOnContent(tabId, headers) {
var contentType = getHeader(headers, "Content-Type");
if (contentType == null)
return NO_DATA;
contentType = contentType.split(";")[0];
if (contentType == "text/n3") {
return {
badge: "N3",
syntax: "text/n3",
fetch: function (tabId = tabId) {
return fetchContent(tabId);
}
};
}
return NO_DATA;
}
function detectData(tabId, headers) {
var linkedData = detectDataOnContent(tabId, headers);
if (linkedData == NO_DATA)
linkedData = detectDataOnLinks(headers);
data = getTabData(tabId);
data.linkedData = linkedData;
updateIcon(tabId);
}
function onHeaders(details) {
console.log(details);
detectData(details.tabId, details.responseHeaders);
}
// listen to received headers
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment