Commit 5251ffe7 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixing Chrome compatibility

parent 76d74ec12e1f
......@@ -36,7 +36,7 @@ import {
ORIGIN_KIND_REDIRECTED,
DocumentStatus
} from "../common/data";
import { Message, activateNegotiated } from "../common/messages";
import { Message, activateTab } from "../common/messages";
/// <reference path="./fallback.d.ts"/>
let F = require("./fallback");
import "chrome";
......@@ -398,7 +398,7 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
allObservations[openedTab.id] = JSON.parse(JSON.stringify(observation));
allObservations[openedTab.id].status = DocumentStatus.active;
onObservedTabUpdated(openedTab.id);
if (isNegotiated) activateNegotiated(openedTab.id, observation);
activateTab(openedTab.id, observation);
};
let url = isNegotiated
? observation.url
......
......@@ -362,16 +362,16 @@ export function updateCurrentCommand(
}
/**
* Activates a tab with negotiated content
* Activates a tab with observations
* @param id The identifier of a tab
* @param observations The observations for the document
*/
export function activateNegotiated(
export function activateTab(
tabId: number,
observations: DocumentObservations
): void {
chrome.tabs.sendMessage(tabId, {
requestType: "ActivateNegotiated",
requestType: "ActivateTab",
payload: observations
});
}
......@@ -101,15 +101,50 @@ function initializeBrowser(): LDBrowser {
return browser;
}
function onActivatedByNegotiated(
/**
* Event for received messages
* @param request The current request
* @param sender The sender
* @param sendResponse The callback to send responses
*/
function onActivated(
request: Message<any>,
sender: any,
sendResponse: (response: any) => void
) {
if (request.requestType == "ActivateNegotiated") {
chrome.runtime.onMessage.removeListener(onActivatedByNegotiated);
if (request.requestType == "ActivateTab") {
chrome.runtime.onMessage.removeListener(onActivated);
let observation: DocumentObservations = request.payload;
onObservations(observation);
}
}
/**
* Wait for activation from the background
*/
function waitActivation() {
chrome.runtime.onMessage.addListener(onActivated);
}
/**
* When the first observations from the background are received
* @param observation The observation for this tab
*/
function onObservations(observation: DocumentObservations) {
sendHead(observation);
if (
observation.status == DocumentStatus.off ||
observation.status == DocumentStatus.asleep ||
observation.status == DocumentStatus.activeRaw
)
return;
if (
observation.preemptable ||
(observation.negotiated != null && observation.negotiated != undefined)
) {
let browser = initializeBrowser();
browser.onReachedUri(window.location.href);
browser.onReachedUri(window.location.href, observation.origin);
}
}
......@@ -118,25 +153,17 @@ function onActivatedByNegotiated(
*/
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.status == DocumentStatus.off ||
observation.status == DocumentStatus.asleep ||
observation.status == DocumentStatus.activeRaw
)
return;
if (observation.preemptable) {
let browser = initializeBrowser();
browser.onReachedUri(window.location.href, observation.origin);
} else if (
observation.negotiated != null &&
observation.negotiated != undefined
) {
chrome.runtime.onMessage.addListener(onActivatedByNegotiated);
}
});
getObservationsFor(null)
.then((observation: DocumentObservations) => {
if (observation == null) {
waitActivation();
} else {
onObservations(observation);
}
})
.catch((reaons: any) => {
waitActivation();
});
}
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