Commit 28fcbcae authored by Laurent Wouters's avatar Laurent Wouters
Browse files

Transform into a web extension

parent 15a9d1e526f4
......@@ -12,10 +12,48 @@ function injectApplication() {
var appContent = document.createElement("div");
appContent.id = ID;
appContent.style.zIndex = zIndex + 10;
appContent.appendChild(document.createTextNode("Here!"));
appContent.appendChild(document.createTextNode("Loading ..."));
document.body.insertBefore(appContent, document.body.firstChild);
}
function doFetchContent(contentType) {
return new Promise(function (resolve, reject) {
resolve({
contentType: contentType,
content: document.body.innerText
});
});
}
function doFetchLink(link) {
return new Promise(function (resolve, reject) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status != 200) {
reject("HTTP error: " + xmlHttp.status);
}
var ct = xmlHttp.getResponseHeader("Content-Type");
resolve({
contentType: ct,
content: xmlHttp.responseText
});
}
}
xmlHttp.open("GET", link.url, true);
xmlHttp.setRequestHeader("Accept", link.type);
xmlHttp.send();
});
}
function injectContent(data) {
var root = document.getElementById(ID);
while (root.hasChildNodes()) {
root.removeChild(root.lastChild);
}
root.appendChild(document.createTextNode(data.content));
}
function onUpdateDeactivate() {
var root = document.getElementById(ID);
if (root == null)
......@@ -23,16 +61,28 @@ function onUpdateDeactivate() {
root.parentNode.removeChild(root);
}
function onUpdateActivate(data) {
function onUpdateActivate(linkedData) {
onUpdateDeactivate();
injectApplication();
var fetch = null;
if (linkedData.target == "content") {
fetch = doFetchContent(linkedData.syntax);
} else {
fetch = doFetchLink(linkedData.target)
}
fetch.then(function (result) {
injectContent(result);
}).catch(function (reason) {
var root = document.getElementById(ID);
root.innerText = reason;
});
}
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
var activate = request.isActive && request.syntax != "";
var activate = request.isActive && request.linkedData.syntax != "";
if (activate)
onUpdateActivate(request);
onUpdateActivate(request.linkedData);
else
onUpdateDeactivate();
});
......
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