Commit 9c83f14c authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixed linting issues with ldbrowser

parent 9bd560bb123c
...@@ -31,11 +31,9 @@ import { ...@@ -31,11 +31,9 @@ import {
ObservableContent, ObservableContent,
observeContent, observeContent,
getAcceptRdf, getAcceptRdf,
ORIGIN_KIND_DIRECT,
ORIGIN_KIND_LINKED_HTML,
ORIGIN_KIND_REDIRECTED,
DocumentStatus, DocumentStatus,
DocumentObservations DocumentObservations,
OriginKind
} from "../common/data"; } from "../common/data";
import { Message, activateTab } from "../common/messages"; import { Message, activateTab } from "../common/messages";
/// <reference path="./fallback.d.ts"/> /// <reference path="./fallback.d.ts"/>
...@@ -54,7 +52,7 @@ let allObservations: ObservedResourceRegistry = {}; ...@@ -54,7 +52,7 @@ let allObservations: ObservedResourceRegistry = {};
/** /**
* The observations for an opening tab * The observations for an opening tab
*/ */
let openingObservations: DocumentObservations = null; let openingObservations: DocumentObservations | null = null;
/** /**
* The reference view registry * The reference view registry
*/ */
...@@ -65,7 +63,9 @@ let registry: definition.ViewRegistry = new definition.ViewRegistry(); ...@@ -65,7 +63,9 @@ let registry: definition.ViewRegistry = new definition.ViewRegistry();
let cache: definition.ViewResourceCache = {}; let cache: definition.ViewResourceCache = {};
reloadRegistryFromStorage(registry) reloadRegistryFromStorage(registry)
.then((registry: definition.ViewRegistry) => {}) .then((registry: definition.ViewRegistry) => {
/* do nothing */
})
.catch((reason: string) => { .catch((reason: string) => {
console.log(reason); console.log(reason);
}); });
...@@ -77,8 +77,8 @@ reloadRegistryFromStorage(registry) ...@@ -77,8 +77,8 @@ reloadRegistryFromStorage(registry)
function onObservedTabUpdated(tabId: number): void { function onObservedTabUpdated(tabId: number): void {
let observation = resolveObservationsForTab(allObservations, tabId); let observation = resolveObservationsForTab(allObservations, tabId);
let isLit = let isLit =
observation.status != DocumentStatus.off || hasDetectedData(observation); observation.status !== DocumentStatus.off || hasDetectedData(observation);
let hasPopup = observation.status == DocumentStatus.active; let hasPopup = observation.status === DocumentStatus.active;
if (isLit) { if (isLit) {
chrome.pageAction.show(tabId); chrome.pageAction.show(tabId);
...@@ -97,12 +97,14 @@ function onObservedTabUpdated(tabId: number): void { ...@@ -97,12 +97,14 @@ function onObservedTabUpdated(tabId: number): void {
* @param name The name of a header * @param name The name of a header
*/ */
function getHeader( function getHeader(
headers: chrome.webRequest.HttpHeader[], headers: chrome.webRequest.HttpHeader[] | undefined,
name: string name: string
): string { ): string | null {
for (var i = 0; i != headers.length; i++) { if (headers === undefined) return null;
if (headers[i].name == name) { for (let i = 0; i !== headers.length; i++) {
return headers[i].value; if (headers[i].name === name) {
let v = headers[i].value;
if (v !== undefined) return v;
} }
} }
return null; return null;
...@@ -119,8 +121,8 @@ function setHeader( ...@@ -119,8 +121,8 @@ function setHeader(
name: string, name: string,
value: string value: string
): void { ): void {
for (var i = 0; i != headers.length; i++) { for (let i = 0; i !== headers.length; i++) {
if (headers[i].name == name) { if (headers[i].name === name) {
headers[i].value = value; headers[i].value = value;
} }
} }
...@@ -135,31 +137,30 @@ function onSendingHeaders( ...@@ -135,31 +137,30 @@ function onSendingHeaders(
details: chrome.webRequest.WebRequestHeadersDetails details: chrome.webRequest.WebRequestHeadersDetails
): chrome.webRequest.BlockingResponse { ): chrome.webRequest.BlockingResponse {
if ( if (
details.tabId == -1 || // invalid tab details.tabId === -1 || // invalid tab
details.frameId != 0 || // not the main top-level frame details.frameId !== 0 || // not the main top-level frame
details.method != "GET" || // not a GET method details.method !== "GET" || // not a GET method
details.type != "main_frame" // not the top-level document details.type !== "main_frame" // not the top-level document
) ) {
return {}; return {};
}
let observation = allObservations[details.tabId]; let observation = allObservations[details.tabId];
if (observation == null || observation == undefined) { if (observation === undefined) {
if (openingObservations == null) return {}; if (openingObservations === null) return {};
// this is tab opening from the extension // this is tab opening from the extension
observation = openingObservations; observation = openingObservations;
} }
// do not request RDF for off and asleep tabs // do not request RDF for off and asleep tabs
if ( if (
observation.status == DocumentStatus.off || observation.status === DocumentStatus.off ||
observation.status == DocumentStatus.asleep observation.status === DocumentStatus.asleep
) ) {
return {}; return {};
}
if ( if (observation.origin.kind !== OriginKind.Redirected) {
!observation.origin ||
observation.origin.kind != ORIGIN_KIND_REDIRECTED
) {
observation.origin = { observation.origin = {
kind: ORIGIN_KIND_DIRECT, kind: OriginKind.Direct,
url: details.url url: details.url
}; };
} }
...@@ -167,14 +168,13 @@ function onSendingHeaders( ...@@ -167,14 +168,13 @@ function onSendingHeaders(
if (observation.preemptable) { if (observation.preemptable) {
// this tab has been preempted // this tab has been preempted
let headers = details.requestHeaders; let headers = details.requestHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Accept", getAcceptRdf(false)); setHeader(headers, "Accept", getAcceptRdf(false));
return { requestHeaders: headers }; return { requestHeaders: headers };
} else if ( } else if (observation.negotiated !== null) {
observation.negotiated != null &&
observation.negotiated != undefined
) {
// this tab has negotiated content // this tab has negotiated content
let headers = details.requestHeaders; let headers = details.requestHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Accept", observation.negotiated.contentType); setHeader(headers, "Accept", observation.negotiated.contentType);
return { requestHeaders: headers }; return { requestHeaders: headers };
} }
...@@ -189,21 +189,22 @@ function onHeadersReceived( ...@@ -189,21 +189,22 @@ function onHeadersReceived(
details: chrome.webRequest.WebResponseHeadersDetails details: chrome.webRequest.WebResponseHeadersDetails
): chrome.webRequest.BlockingResponse { ): chrome.webRequest.BlockingResponse {
if ( if (
details.tabId == -1 || // invalid tab details.tabId === -1 || // invalid tab
details.frameId != 0 || // not the main top-level frame details.frameId !== 0 || // not the main top-level frame
details.method != "GET" || // not a GET method details.method !== "GET" || // not a GET method
details.statusCode < 200 || // not an OK details.statusCode < 200 || // not an OK
details.statusCode >= 400 || // redirect or failure no handled details.statusCode >= 400 || // redirect or failure no handled
details.type != "main_frame" // not the top-level document details.type !== "main_frame" // not the top-level document
) ) {
return {}; return {};
}
if (details.statusCode >= 300) { if (details.statusCode >= 300) {
let observation = allObservations[details.tabId]; let observation = allObservations[details.tabId];
if (details.statusCode == 303 && observation) { if (details.statusCode === 303 && observation) {
// mark as negotiated // mark as negotiated
observation.origin = { observation.origin = {
kind: ORIGIN_KIND_REDIRECTED, kind: OriginKind.Redirected,
url: details.url url: details.url
}; };
} }
...@@ -223,24 +224,27 @@ function onHeadersReceived( ...@@ -223,24 +224,27 @@ function onHeadersReceived(
} }
let contentType = getHeader(details.responseHeaders, "Content-Type"); let contentType = getHeader(details.responseHeaders, "Content-Type");
if (contentType == null || contentType == undefined) { if (contentType === null) {
// no content // no content
return {}; return {};
} }
index = contentType.indexOf(";"); index = contentType.indexOf(";");
if (index >= 0) contentType = contentType.substring(0, index); if (index >= 0) contentType = contentType.substring(0, index);
let linkHeader = getHeader(details.responseHeaders, "Link");
let observable: ObservableContent = { let observable: ObservableContent = {
content: null, content: "",
contentType: contentType, contentType: contentType,
linkHeader: getHeader(details.responseHeaders, "Link"), linkHeader: linkHeader !== null ? linkHeader : "",
url: details.url url: details.url
}; };
let obs = observeContent(observable, { let obs = observeContent(observable, {
kind: ORIGIN_KIND_DIRECT, kind: OriginKind.Direct,
url: details.url url: details.url
}); });
let observation = resolveObservationsForTab(allObservations, details.tabId); let observation = resolveObservationsForTab(allObservations, details.tabId);
if (observation.origin == null) observation.origin = obs.origin; if (observation.origin.kind === OriginKind.Unkown) {
observation.origin = obs.origin;
}
observation.url = obs.url; observation.url = obs.url;
observation.primaryTopic = obs.primaryTopic; observation.primaryTopic = obs.primaryTopic;
observation.sources = obs.sources; observation.sources = obs.sources;
...@@ -249,6 +253,7 @@ function onHeadersReceived( ...@@ -249,6 +253,7 @@ function onHeadersReceived(
if (obs.preemptable) { if (obs.preemptable) {
// modify header // modify header
let headers = details.responseHeaders; let headers = details.responseHeaders;
if (headers === undefined) headers = [];
setHeader(headers, "Content-Type", "text/plain"); setHeader(headers, "Content-Type", "text/plain");
return { responseHeaders: headers }; return { responseHeaders: headers };
} }
...@@ -276,7 +281,7 @@ function onHeadersReceived( ...@@ -276,7 +281,7 @@ function onHeadersReceived(
function onCompleted( function onCompleted(
details: chrome.webNavigation.WebNavigationFramedCallbackDetails details: chrome.webNavigation.WebNavigationFramedCallbackDetails
): void { ): void {
if (details.tabId == -1 || details.frameId != 0) return; if (details.tabId === -1 || details.frameId !== 0) return;
onObservedTabUpdated(details.tabId); onObservedTabUpdated(details.tabId);
} }
...@@ -297,34 +302,38 @@ F.registerNavigations(onCompleted); ...@@ -297,34 +302,38 @@ F.registerNavigations(onCompleted);
chrome.runtime.onMessage.addListener( chrome.runtime.onMessage.addListener(
(request: Message<any>, sender, sendResponse) => { (request: Message<any>, sender, sendResponse) => {
if (request.requestType == "SubmitHtmlLinks") { if (request.requestType === "SubmitHtmlLinks") {
if (sender.tab === undefined || sender.tab.id === undefined) return;
let observation = resolveObservationsForTab( let observation = resolveObservationsForTab(
allObservations, allObservations,
sender.tab.id sender.tab.id
); );
if (observation.primaryTopic == null || observation.primaryTopic == "") { if (observation.primaryTopic === "") {
observation.primaryTopic = detectTopicOnlinks(request.payload); let topic = detectTopicOnlinks(request.payload);
observation.primaryTopic = topic !== null ? topic : "";
} }
observation.sources = observation.sources.concat( observation.sources = observation.sources.concat(
detectDataOnLinks(request.payload, { detectDataOnLinks(request.payload, {
kind: ORIGIN_KIND_LINKED_HTML, kind: OriginKind.HtmlLink,
url: observation.url url: observation.url
}) })
); );
onObservedTabUpdated(sender.tab.id); onObservedTabUpdated(sender.tab.id);
} else if (request.requestType == "GetTabId") { } else if (request.requestType === "GetTabId") {
if (sender.tab === undefined || sender.tab.id === undefined) return;
sendResponse(sender.tab.id); sendResponse(sender.tab.id);
} else if (request.requestType == "GetObservations") { } else if (request.requestType === "GetObservations") {
let tabId = request.payload; let tabId = request.payload;
if (tabId == null || tabId == undefined) { if (tabId === null || tabId === undefined) {
// deduce from the sender // deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id; tabId = sender.tab.id;
} }
let observation = resolveObservationsForTab(allObservations, tabId); let observation = resolveObservationsForTab(allObservations, tabId);
sendResponse(observation); sendResponse(observation);
} else if (request.requestType == "GetViewRegistry") { } else if (request.requestType === "GetViewRegistry") {
sendResponse(registry); sendResponse(registry);
} else if (request.requestType == "AddNewSource") { } else if (request.requestType === "AddNewSource") {
addViewSource(registry, request.payload) addViewSource(registry, request.payload)
.then((newRegistry: definition.ViewRegistry) => { .then((newRegistry: definition.ViewRegistry) => {
sendResponse(newRegistry); sendResponse(newRegistry);
...@@ -333,7 +342,7 @@ chrome.runtime.onMessage.addListener( ...@@ -333,7 +342,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason); console.log("Failed to reload registry: " + reason);
}); });
return true; return true;
} else if (request.requestType == "RemoveSource") { } else if (request.requestType === "RemoveSource") {
removeViewSource(registry, request.payload) removeViewSource(registry, request.payload)
.then((newRegistry: definition.ViewRegistry) => { .then((newRegistry: definition.ViewRegistry) => {
sendResponse(newRegistry); sendResponse(newRegistry);
...@@ -342,7 +351,7 @@ chrome.runtime.onMessage.addListener( ...@@ -342,7 +351,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason); console.log("Failed to reload registry: " + reason);
}); });
return true; return true;
} else if (request.requestType == "ReloadViewRegistry") { } else if (request.requestType === "ReloadViewRegistry") {
cache = {}; cache = {};
reloadRegistryFromStorage(registry) reloadRegistryFromStorage(registry)
.then((newRegistry: definition.ViewRegistry) => { .then((newRegistry: definition.ViewRegistry) => {
...@@ -352,7 +361,7 @@ chrome.runtime.onMessage.addListener( ...@@ -352,7 +361,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to reload registry: " + reason); console.log("Failed to reload registry: " + reason);
}); });
return true; return true;
} else if (request.requestType == "GetResourceContent") { } else if (request.requestType === "GetResourceContent") {
definition definition
.getResourceContent(cache, request.payload) .getResourceContent(cache, request.payload)
.then((content: string) => { .then((content: string) => {
...@@ -362,7 +371,7 @@ chrome.runtime.onMessage.addListener( ...@@ -362,7 +371,7 @@ chrome.runtime.onMessage.addListener(
console.log("Failed to fetch the resource: " + reason); console.log("Failed to fetch the resource: " + reason);
}); });
return true; return true;
} else if (request.requestType == "FetchObservable") { } else if (request.requestType === "FetchObservable") {
fetchObservableAt(request.payload) fetchObservableAt(request.payload)
.then((content: ObservableContent) => { .then((content: ObservableContent) => {
sendResponse({ ok: content, error: null }); sendResponse({ ok: content, error: null });
...@@ -371,25 +380,33 @@ chrome.runtime.onMessage.addListener( ...@@ -371,25 +380,33 @@ chrome.runtime.onMessage.addListener(
sendResponse({ ok: null, error: reason }); sendResponse({ ok: null, error: reason });
}); });
return true; return true;
} else if (request.requestType == "TurnOffForTab") { } else if (request.requestType === "TurnOffForTab") {
let tabId = request.payload; let tabId = request.payload;
if (tabId == null || tabId == undefined) { if (tabId === null || tabId === undefined) {
// deduce from the sender // deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id; tabId = sender.tab.id;
} }
let observation = allObservations[tabId]; let observation = allObservations[tabId];
if (observation == undefined) sendResponse(null); if (observation === undefined) {
sendResponse(null);
return;
}
observation.status = DocumentStatus.asleep; observation.status = DocumentStatus.asleep;
onObservedTabUpdated(tabId); onObservedTabUpdated(tabId);
sendResponse(observation); sendResponse(observation);
} else if (request.requestType == "ToggleRawForTab") { } else if (request.requestType === "ToggleRawForTab") {
let tabId = request.payload; let tabId = request.payload;
if (tabId == null || tabId == undefined) { if (tabId === null || tabId === undefined) {
// deduce from the sender // deduce from the sender
if (sender.tab === undefined || sender.tab.id === undefined) return;
tabId = sender.tab.id; tabId = sender.tab.id;
} }
let observation = allObservations[tabId]; let observation = allObservations[tabId];
if (observation == undefined) sendResponse(null); if (observation === undefined) {
sendResponse(null);
return;
}
observation.status = DocumentStatus.activeRaw; observation.status = DocumentStatus.activeRaw;
onObservedTabUpdated(tabId); onObservedTabUpdated(tabId);
sendResponse(observation); sendResponse(observation);
...@@ -398,12 +415,13 @@ chrome.runtime.onMessage.addListener( ...@@ -398,12 +415,13 @@ chrome.runtime.onMessage.addListener(
); );
chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => { chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
if (tab.id === undefined) return;
let observation = resolveObservationsForTab(allObservations, tab.id); let observation = resolveObservationsForTab(allObservations, tab.id);
if (observation.status == DocumentStatus.off) { if (observation.status === DocumentStatus.off) {
// this tab was off // this tab was off
let isNegotiated = let isNegotiated = observation.negotiated !== null;
observation.negotiated != null && observation.negotiated != undefined;
let callback = (openedTab: chrome.tabs.Tab) => { let callback = (openedTab: chrome.tabs.Tab) => {
if (openedTab.id === undefined || openingObservations === null) return;
allObservations[openedTab.id] = openingObservations; allObservations[openedTab.id] = openingObservations;
openingObservations = null; openingObservations = null;
onObservedTabUpdated(openedTab.id); onObservedTabUpdated(openedTab.id);
...@@ -415,6 +433,7 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => { ...@@ -415,6 +433,7 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
"ldbrowser/index.html?target=" + encodeURIComponent(observation.url) "ldbrowser/index.html?target=" + encodeURIComponent(observation.url)
); );
openingObservations = JSON.parse(JSON.stringify(observation)); openingObservations = JSON.parse(JSON.stringify(observation));
if (openingObservations === null) return;
openingObservations.status = DocumentStatus.active; openingObservations.status = DocumentStatus.active;
let promise: any = chrome.tabs.create( let promise: any = chrome.tabs.create(
{ {
...@@ -426,12 +445,12 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => { ...@@ -426,12 +445,12 @@ chrome.pageAction.onClicked.addListener((tab: chrome.tabs.Tab) => {
}, },
callback callback
); );
if (promise != undefined && promise != null) { if (promise !== undefined && promise !== null) {
promise.then(callback); promise.then(callback);
} }
} else if ( } else if (
observation.status == DocumentStatus.asleep || observation.status === DocumentStatus.asleep ||
observation.status == DocumentStatus.activeRaw observation.status === DocumentStatus.activeRaw
) { ) {
observation.status = DocumentStatus.active; observation.status = DocumentStatus.active;
onObservedTabUpdated(tab.id); onObservedTabUpdated(tab.id);
......
...@@ -33,8 +33,8 @@ import { ...@@ -33,8 +33,8 @@ import {
ResourceUserCommand, ResourceUserCommand,
DocumentSource, DocumentSource,
DocumentObservations, DocumentObservations,
ORIGIN_KIND_DIRECT, Origin,
Origin OriginKind
} from "../common/data"; } from "../common/data";
import { import {
getResourceContent, getResourceContent,
...@@ -52,9 +52,9 @@ function getBrowserLanguage(): application.Language { ...@@ -52,9 +52,9 @@ function getBrowserLanguage(): application.Language {
if (i > 0) lang = lang.substring(0, i); if (i > 0) lang = lang.substring(0, i);
let result = LANGUAGES.find( let result = LANGUAGES.find(
(language: application.Language) => (language: application.Language) =>
language.iso639_1 == lang || language.iso639_2 == lang language.iso639_1 === lang || language.iso639_2 === lang
); );
if (result != null && result != undefined) return result; if (result !== undefined) return result;
return application.NO_LANGUAGE; return application.NO_LANGUAGE;
} }
...@@ -122,7 +122,7 @@ export interface LDBrowser { ...@@ -122,7 +122,7 @@ export interface LDBrowser {
/** /**
* Gets the current resource * Gets the current resource
*/ */
getCurrentResource(): ResourceData; getCurrentResource(): ResourceData | null;
/** /**
* Sets the new user command for the current resouce * Sets the new user command for the current resouce
* @param command The new user command * @param command The new user command
...@@ -159,7 +159,14 @@ class LDBrowserImpl implements LDBrowser { ...@@ -159,7 +159,14 @@ class LDBrowserImpl implements LDBrowser {
// Listens to history state events from the browser // Listens to history state events from the browser
let self = this; let self = this;
window.onpopstate = function(event) { window.onpopstate = function(event) {
self.onReachedUri(event.state.uri); self
.onReachedUri(event.state.uri)
.then((value: void) => {
// do nothing
})
.catch((reason: any) => {
console.log(reason);
});
}; };
}