Commit 3f4c9d89 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Transform into a web extension

parent ecde72bff2b6
......@@ -8,8 +8,9 @@
},
"permissions": [
"tabs",
"activeTab",
"webRequest"
"webRequest",
"webNavigation",
"*://*/*"
],
"browser_action": {
"default_icon": "icons/browser_action_disabled.png",
......
var NO_DATA = {
badge: "",
syntax: "",
fetch: function () {
return new Promise(function (resolve, reject) {
reject("No data!");
});
}
syntax: null,
target: null
};
var allTabs = {};
var MIME = {
"text/n3": "N3",
"application/n-triples": "N3",
"application/n-quads": "N4",
"text/turtle": "TTL",
"application/rdf+xml": "RDF",
"application/ld+json": "JSON",
"application/trig": "TRIG"
}
function getTabData(id) {
if (!allTabs.hasOwnProperty(id)) {
allTabs[id] = {
......@@ -113,13 +120,11 @@ function detectDataOnLinks(headers) {
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") {
if (links[j].rel == "alternate" && MIME.hasOwnProperty(links[j].type)) {
return {
badge: "N3",
badge: MIME[links[j].type],
syntax: links[j].type,
fetch: function (url = links[j].url) {
return fetchAt(url);
}
target: links[j]
};
}
}
......@@ -131,30 +136,34 @@ function detectDataOnContent(tabId, headers) {
if (contentType == null)
return NO_DATA;
contentType = contentType.split(";")[0];
if (contentType == "text/n3") {
if (MIME.hasOwnProperty(contentType)) {
return {
badge: "N3",
syntax: "text/n3",
fetch: function (tabId = tabId) {
return fetchContent(tabId);
}
badge: MIME[contentType],
syntax: contentType,
target: "content"
};
}
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 onHeadersReceived(details) {
data = getTabData(details.tabId);
if (data.linkedData != NO_DATA)
// already detected something
return;
data.linkedData = detectDataOnContent(details.tabId, details.responseHeaders);
if (data.linkedData == NO_DATA)
data.linkedData = detectDataOnLinks(details.responseHeaders);
updateIcon(details.tabId);
}
function onHeaders(details) {
detectData(details.tabId, details.responseHeaders);
function onBeforeNavigate(details) {
data = getTabData(details.tabId);
data.linkedData = NO_DATA;
updateIcon(details.tabId);
}
// listen to received headers
chrome.webRequest.onHeadersReceived.addListener(onHeaders, { urls: ["<all_urls>"] }, ["responseHeaders"]);
chrome.webRequest.onHeadersReceived.addListener(onHeadersReceived, { urls: ["<all_urls>"] }, ["responseHeaders"]);
// listen to received headers
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate, { urls: ["<all_urls>"] });
\ No newline at end of file
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
});
\ No newline at end of file
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