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

[fix] Preempts the web page for recognized RDF MIME types

parent 2e46b3d8edc5
......@@ -159,6 +159,12 @@ function onHeadersReceived(
// modify header
let headers = details.responseHeaders;
setHeader(headers, "Content-Type", "text/plain");
// set the popup
chrome.pageAction.setPopup({
tabId: details.tabId,
popup: chrome.extension.getURL("popup/index.html")
});
chrome.pageAction.show(details.tabId);
return { responseHeaders: headers };
}
// if there are still nothing, try to probe with HTTP content negotiation
......
......@@ -88,6 +88,16 @@ export interface LDBrowserEventHandler {
onError(description: string): void;
}
/**
* The parameters for the browser
*/
export interface LDBrowserParameters {
/**
* Whether to use the managed history
*/
managedHistory: boolean;
}
/**
* The interface for an LD browser
*/
......@@ -120,16 +130,21 @@ export interface LDBrowser {
/**
* Creates a new LD browser
* @param handler The handler for the events
* @param parameters The configuration for the browser
*/
export function newBrowser(handler: LDBrowserEventHandler): LDBrowser {
return new LDBrowserImpl(handler);
export function newBrowser(
handler: LDBrowserEventHandler,
parameters: LDBrowserParameters
): LDBrowser {
return new LDBrowserImpl(handler, parameters);
}
/**
* The data of an LD browser
*/
class LDBrowserImpl implements LDBrowser {
constructor(handler: LDBrowserEventHandler) {
constructor(handler: LDBrowserEventHandler, parameters: LDBrowserParameters) {
this.parameters = parameters;
this.handler = handler;
this.renderer = implementation.newRenderer(getResourceContent);
this.currentData = null;
......@@ -137,8 +152,19 @@ class LDBrowserImpl implements LDBrowser {
this.cacheStores = {};
this.onRequestNavigateTo = this.onRequestNavigateTo.bind(this);
this.onSelectAsPrimaryTopic = this.onSelectAsPrimaryTopic.bind(this);
if (parameters.managedHistory) {
// Listens to history state events from the browser
let self = this;
window.onpopstate = function(event) {
self.onReachedUri(event.state.uri);
};
}
}
/**
* The configuration for the browser
*/
private parameters: LDBrowserParameters;
/**
* The handler for the events
*/
......@@ -447,13 +473,17 @@ class LDBrowserImpl implements LDBrowser {
* @param uri The URI to navigate to
*/
public navigateTo(uri: string): void {
history.pushState(
{ uri: uri },
uri,
chrome.extension.getURL("ldbrowser/index.html?target=") +
encodeURIComponent(uri)
);
this.onReachedUri(uri);
if (this.parameters.managedHistory) {
history.pushState(
{ uri: uri },
uri,
chrome.extension.getURL("ldbrowser/index.html?target=") +
encodeURIComponent(uri)
);
this.onReachedUri(uri);
} else {
window.location.assign(uri);
}
}
/**
......
......@@ -133,6 +133,11 @@ class MainViewer implements Viewer {
*/
public onUpdate() {
let root = document.getElementById("root");
if (root == null) {
root = document.createElement("div");
root.id = "root";
document.body.appendChild(root);
}
while (root.hasChildNodes()) {
root.removeChild(root.lastChild);
}
......@@ -148,6 +153,11 @@ class MainViewer implements Viewer {
for (var i = 0; i != scripts.length; i++) {
if (scripts[i].id != "main-script") document.body.removeChild(scripts[i]);
}
let pres = getAllNodes(document.body, "pre");
for (var i = 0; i != pres.length; i++) {
document.body.removeChild(pres[i]);
}
this.render(root);
}
......
......@@ -18,9 +18,12 @@
* with CubicWeb. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
import { DocumentObservations, findLinksInDocument } from "../common/data";
import { getObservationsFor } from "../common/messages";
import "chrome";
import { Message, getObservationsFor } from "../common/messages";
import { DocumentObservations, findLinksInDocument } from "../common/data";
import { Viewer, newViewer } from "../common/viewer";
import { LDBrowser, newBrowser } from "../common/browser";
require("../common/view-defaults-impl");
/**
* Sends the links in the HTML head to the background
......@@ -40,113 +43,41 @@ function sendHead(observation: DocumentObservations): void {
}
/**
* Identifier of the DOM element for the injected application
*/
const ID: string = "linked-data-injectable-app";
/**
* The current injectable application
*/
var injectable: HTMLDivElement = null;
/**
* The injected iframe element
*/
var injectableFrame: HTMLIFrameElement = null;
/**
* Get the maximum z-index value in this page
*/
function maxZIndex(): number {
return Array.from(document.querySelectorAll("body *"))
.map(a => parseFloat(window.getComputedStyle(a).zIndex))
.filter(a => !isNaN(a))
.reduce(function(acc, value) {
return acc > value ? acc : value;
}, 0);
}
function getAllNodes(parent: Element, tag: string): Element[] {
let result = [];
let nodes = parent.getElementsByTagName(tag);
for (var i = 0; i != nodes.length; i++) {
result.push(nodes.item(i));
}
return result;
}
/**
* Inject the application stub into the current page
* Initializes the LD browser
*/
function injectApplication() {
if (injectable != null) return;
let zIndex = maxZIndex();
injectable = document.createElement("div");
injectable.id = ID;
injectable.style.position = "absolute";
injectable.style.left = "0px";
injectable.style.top = "0px";
injectable.style.display = "flex";
injectable.style.width = "100%";
injectable.style.height = "100%";
injectable.style.flexDirection = "column";
injectable.style.overflow = "hidden";
injectable.style.zIndex = (zIndex + 10).toString();
injectableFrame = document.createElement("iframe");
injectableFrame.src = chrome.extension.getURL(
"ldbrowser/index.html?target=" + encodeURIComponent(window.location.href)
);
injectableFrame.style.flexGrow = "1";
injectableFrame.style.margin = "0";
injectableFrame.style.padding = "0";
injectable.appendChild(injectableFrame);
let links = getAllNodes(document.head, "link");
for (var i = 0; i != links.length; i++) {
document.head.removeChild(links[i]);
}
let styles = getAllNodes(document.head, "style");
for (var i = 0; i != styles.length; i++) {
document.head.removeChild(styles[i]);
}
let scripts = getAllNodes(document.body, "script");
for (var i = 0; i != scripts.length; i++) {
if (scripts[i].id != "main-script") document.body.removeChild(scripts[i]);
}
for (var i = 0; i != document.body.childNodes.length; i++) {
let child = document.body.childNodes.item(i);
if (child.nodeType == child.ELEMENT_NODE) {
let element = child as HTMLElement;
(element as any).oldDisplay = element.style.display;
element.style.display = "none";
function initializeBrowser() {
// The application view to use
let viewer: Viewer = newViewer();
// The LD browser instance
let browser: LDBrowser = newBrowser(viewer, {
managedHistory: false
});
// Listen to messages from the background
chrome.runtime.onMessage.addListener(function(
request: Message<any>,
sender,
sendResponse
) {
if (request.requestType == "GetCurrentResource") {
sendResponse(browser.getCurrentResource());
} else if (request.requestType == "UpdateCurrentCommand") {
browser.setCommand(request.payload);
}
}
document.body.insertBefore(injectable, document.body.firstChild);
});
browser.onReachedUri(window.location.href);
}
/**
* Remove the application
* Initializes the browser
*/
function removeApplication(): void {
if (injectable == null) return;
document.body.removeChild(injectable);
injectable = null;
injectableFrame = null;
for (var i = 0; i != document.body.childNodes.length; i++) {
let child = document.body.childNodes.item(i);
if (child.nodeType == child.ELEMENT_NODE) {
let element = child as HTMLElement;
element.style.display = (element as any).oldDisplay;
function main() {
// Gets data about this tab from the background and reply with the HTML head data
getObservationsFor(null).then((observation: DocumentObservations) => {
sendHead(observation);
if (observation.preemptable) {
initializeBrowser();
}
}
});
}
/**
* Gets data about this tab from the background and reply with the HTML head data
*/
getObservationsFor(null).then((observation: DocumentObservations) => {
sendHead(observation);
if (observation.preemptable) {
injectApplication();
}
});
main();
......@@ -20,9 +20,9 @@
import "chrome";
import { getMyTabId, Message } from "../common/messages";
import { Viewer, newViewer } from "./viewer";
import { LDBrowser, newBrowser } from "./browser";
require("./view-defaults-impl");
import { Viewer, newViewer } from "../common/viewer";
import { LDBrowser, newBrowser } from "../common/browser";
require("../common/view-defaults-impl");
/**
* Get the value of an HTTP parameter
......@@ -38,35 +38,29 @@ function getParameterByName(name: string) {
}
/**
* The current application
* Initializes the LD browser
*/
const CURRENT_VIEWER: Viewer = newViewer();
/**
* The data for the browser
*/
const CURRENT_BROWSER: LDBrowser = newBrowser(CURRENT_VIEWER);
/**
* Listen to messages from the background
*/
chrome.runtime.onMessage.addListener(function(
request: Message<any>,
sender,
sendResponse
) {
if (request.requestType == "GetCurrentResource") {
sendResponse(CURRENT_BROWSER.getCurrentResource());
} else if (request.requestType == "UpdateCurrentCommand") {
CURRENT_BROWSER.setCommand(request.payload);
}
});
/**
* Listens to history state events from the browser
*/
window.onpopstate = function(event) {
CURRENT_BROWSER.onReachedUri(event.state.uri);
};
function initializeBrowser() {
// The application view to use
let viewer: Viewer = newViewer();
// The LD browser instance
let browser: LDBrowser = newBrowser(viewer, {
managedHistory: true
});
// Listen to messages from the background
chrome.runtime.onMessage.addListener(function(
request: Message<any>,
sender,
sendResponse
) {
if (request.requestType == "GetCurrentResource") {
sendResponse(browser.getCurrentResource());
} else if (request.requestType == "UpdateCurrentCommand") {
browser.setCommand(request.payload);
}
});
browser.navigateTo(getParameterByName("target"));
}
/**
* Initializes the browser
......@@ -79,8 +73,7 @@ function main() {
});
chrome.pageAction.show(tabId);
});
let target = getParameterByName("target");
CURRENT_BROWSER.navigateTo(target);
initializeBrowser();
}
main();
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