main.ts 2.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*******************************************************************************
 * Copyright 2003-2018 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 * contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 *
 * This file is part of CubicWeb.
 *
 * CubicWeb is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * CubicWeb is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

Laurent Wouters's avatar
Laurent Wouters committed
21
import "chrome";
22
23
24
import { getMyTabId, Message } from "../common/messages";
import { Viewer, newViewer } from "./viewer";
import { LDBrowser, newBrowser } from "./browser";
Laurent Wouters's avatar
Laurent Wouters committed
25
26
require("./view-defaults-impl");

Laurent Wouters's avatar
Laurent Wouters committed
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
 * Get the value of an HTTP parameter
 * @param name The name of the parameter to retrieve
 */
function getParameterByName(name: string) {
  name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
    results = regex.exec(location.search);
  return results === null
    ? ""
    : decodeURIComponent(results[1].replace(/\+/g, " "));
}

40
41
42
/**
 * The current application
 */
43
const CURRENT_VIEWER: Viewer = newViewer();
Laurent Wouters's avatar
Laurent Wouters committed
44
45
46
/**
 * The data for the browser
 */
47
const CURRENT_BROWSER: LDBrowser = newBrowser(CURRENT_VIEWER);
48

Laurent Wouters's avatar
Laurent Wouters committed
49
50
51
52
53
54
55
56
57
/**
 * Listen to messages from the background
 */
chrome.runtime.onMessage.addListener(function(
  request: Message<any>,
  sender,
  sendResponse
) {
  if (request.requestType == "GetCurrentResource") {
58
    sendResponse(CURRENT_BROWSER.getCurrentResource());
Laurent Wouters's avatar
Laurent Wouters committed
59
  } else if (request.requestType == "UpdateCurrentCommand") {
60
    CURRENT_BROWSER.setCommand(request.payload);
Laurent Wouters's avatar
Laurent Wouters committed
61
62
63
  }
});

64
65
66
67
/**
 * Listens to history state events from the browser
 */
window.onpopstate = function(event) {
68
  CURRENT_BROWSER.onReachedUri(event.state.uri);
69
70
};

71
/**
Laurent Wouters's avatar
Laurent Wouters committed
72
 * Initializes the browser
73
 */
Laurent Wouters's avatar
Laurent Wouters committed
74
function main() {
Laurent Wouters's avatar
Laurent Wouters committed
75
76
77
78
79
80
81
  getMyTabId().then((tabId: number) => {
    chrome.pageAction.setPopup({
      tabId: tabId,
      popup: chrome.extension.getURL("popup/index.html")
    });
    chrome.pageAction.show(tabId);
  });
82
83
  let target = getParameterByName("target");
  CURRENT_BROWSER.navigateTo(target);
84
}
Laurent Wouters's avatar
Laurent Wouters committed
85
86

main();