Commit 1963a0c2 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Transform into a web extension

parent acaf2f111ba0
......@@ -119,11 +119,7 @@ function getHeader(headers, name) {
return null;
}
function detectDataOnLinks(headers) {
linksContent = getHeader(headers, "Link");
if (linksContent == null)
return NO_DATA;
var links = parseLinks(linksContent);
function doDetectDataOnLinks(links) {
for (var j = 0; j != links.length; j++) {
if (links[j].rel == "alternate" && MIME.hasOwnProperty(links[j].type)) {
return {
......@@ -136,6 +132,14 @@ function detectDataOnLinks(headers) {
return NO_DATA;
}
function detectDataOnLinks(headers) {
linksContent = getHeader(headers, "Link");
if (linksContent == null)
return NO_DATA;
var links = parseLinks(linksContent);
return doDetectDataOnLinks(links);
}
function detectDataOnContent(tabId, headers) {
var contentType = getHeader(headers, "Content-Type");
if (contentType == null)
......@@ -152,6 +156,8 @@ function detectDataOnContent(tabId, headers) {
}
function onHeadersReceived(details) {
if (details.tabId == -1)
return;
data = getTabData(details.tabId);
if (data.linkedData != NO_DATA)
// already detected something
......@@ -163,6 +169,8 @@ function onHeadersReceived(details) {
}
function onBeforeNavigate(details) {
if (details.tabId == -1 || details.frameId != 0)
return;
data = getTabData(details.tabId);
data.linkedData = NO_DATA;
updateTab(details.tabId);
......@@ -170,5 +178,16 @@ function onBeforeNavigate(details) {
// listen to received headers
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
// listen to navigation
chrome.webNavigation.onBeforeNavigate.addListener(onBeforeNavigate, { urls: ["<all_urls>"] });
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
data = getTabData(sender.tab.id);
if (request.reqType == "links") {
if (data.linkedData != NO_DATA)
return;
data.linkedData = doDetectDataOnLinks(request.links);
updateTab(sender.tab.id);
}
});
\ No newline at end of file
function onActivated(data) {
}
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
console.log(request);
});
\ No newline at end of file
if (!request.isActive || request.syntax == "")
return;
onActivated(request);
});
function sendHead() {
var links = [];
for (var i = 0; i != document.head.childNodes.length; i++) {
var child = document.head.childNodes[i];
if (child.nodeName == "LINK" && child.hasAttribute("rel") && child.hasAttribute("type") && child.hasAttribute("href")) {
var rel = child.getAttribute("rel");
var type = child.getAttribute("type");
var href = child.getAttribute("href");
if (rel == "meta") {
links.push({
rel: "alternate",
type: type,
url: href
})
}
}
}
chrome.runtime.sendMessage({ reqType: "links", links: links });
}
sendHead();
\ 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