Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsers and state in content #962

Merged
merged 11 commits into from Sep 4, 2018
@@ -1,59 +1,43 @@
/** Background script entry point. */

import * as Controller from "./controller"
import * as keydown_background from "./keydown_background"
import * as CommandLine from "./commandline_background"
import * as BackgroundController from "./controller_background"
import "./lib/browser_proxy_background"

// Send keys to controller
keydown_background.onKeydown.addListener(Controller.acceptKey)
// To eventually be replaced by:
// browser.keyboard.onKeydown.addListener

// Send commandline to controller
CommandLine.onLine.addListener(Controller.acceptExCmd)

// Add various useful modules to the window for debugging
import * as messaging from "./messaging"
import * as excmds from "./.excmds_background.generated"
import * as commandline_background from "./commandline_background"
import * as controller from "./controller"
import * as convert from "./convert"
import * as config from "./config"
import * as dom from "./dom"
import * as hinting_background from "./hinting_background"
import * as download_background from "./download_background"
import * as gobble_mode from "./parsers/gobblemode"
import * as itertools from "./itertools"
import * as keyseq from "./keyseq"
import * as request from "./requests"
import * as native from "./native_background"
import * as msgsafe from "./msgsafe"
import state from "./state"
import * as webext from "./lib/webext"
import { AutoContain } from "./lib/autocontainers"
;(window as any).tri = Object.assign(Object.create(null), {
messaging,
excmds,
commandline_background,
controller,
convert,
config,
dom,
hinting_background,
download_background,
gobble_mode,
itertools,
keydown_background,
native,
keyseq,
request,
msgsafe,
state,
webext,
l: prom => prom.then(console.log).catch(console.error),
})

// Send commandline to controller
commandline_background.onLine.addListener(BackgroundController.acceptExCmd)

// {{{ Clobber CSP

// This should be removed once https://bugzilla.mozilla.org/show_bug.cgi?id=1267027 is fixed
@@ -91,12 +75,12 @@ browser.runtime.onStartup.addListener(_ => {
let hosts = Object.keys(aucmds)
// If there's only one rule and it's "all", no need to check the hostname
if (hosts.length == 1 && hosts[0] == ".*") {
Controller.acceptExCmd(aucmds[hosts[0]])
BackgroundController.acceptExCmd(aucmds[hosts[0]])
} else {
native.run("hostname").then(hostname => {
for (let host of hosts) {
if (hostname.content.match(host)) {
Controller.acceptExCmd(aucmds[host])
BackgroundController.acceptExCmd(aucmds[host])
}
}
})
@@ -1,4 +1,4 @@
import * as Controller from "./controller"
import * as Controller from "./controller_background"
import * as Native from "./native_background"
import Logger from "./logging"
const logger = new Logger("rc")
@@ -4,14 +4,26 @@
// assigned to a name. If you want an import just for its side effects, make
// sure you import it like this:
import "./lib/html-tagged-template"
/* import "./keydown_content" */
/* import "./commandline_content" */
/* import "./excmds_content" */
/* import "./hinting" */
import * as Logging from "./logging"
const logger = new Logging.Logger("content")
logger.debug("Tridactyl content script loaded, boss!")

// Our local state
import { contentState, addContentStateChangedListener } from "./state_content"

// Hook the keyboard up to the controller
import * as ContentController from "./controller_content"
try {
document.body.addEventListener("keydown", ContentController.acceptKey)
} catch (e) {
window.addEventListener("DOMContentLoaded", () => {
document.body.addEventListener("keydown", ContentController.acceptKey)
})
}

// Add various useful modules to the window for debugging
import * as commandline_content from "./commandline_content"
import * as convert from "./convert"
@@ -21,9 +33,7 @@ import * as excmds from "./.excmds_content.generated"
import * as hinting_content from "./hinting"
import * as finding_content from "./finding"
import * as itertools from "./itertools"
import * as keydown_content from "./keydown_content"
import * as messaging from "./messaging"
import * as msgsafe from "./msgsafe"
import state from "./state"
import * as webext from "./lib/webext"
import Mark from "mark.js"
@@ -40,19 +50,19 @@ import * as styling from "./styling"
hinting_content,
finding_content,
itertools,
keydown_content,
logger,
Mark,
keyseq,
messaging,
msgsafe,
state,
webext,
l: prom => prom.then(console.log).catch(console.error),
native,
styling,
})

logger.info("Loaded commandline content?", commandline_content)

// Don't hijack on the newtab page.
if (webext.inContentScript()) {
try {
@@ -141,46 +151,50 @@ config.getAsync("modeindicator").then(mode => {
}
window.addEventListener("mousemove", onMouseOut)
})

try {
// On quick loading pages, the document is already loaded
statusIndicator.textContent = state.mode || "normal"
statusIndicator.textContent = contentState.mode || "normal"
document.body.appendChild(statusIndicator)
document.head.appendChild(style)
} catch (e) {
// But on slower pages we wait for the document to load
window.addEventListener("DOMContentLoaded", () => {
statusIndicator.textContent = state.mode || "normal"
statusIndicator.textContent = contentState.mode || "normal"
document.body.appendChild(statusIndicator)
document.head.appendChild(style)
})
}

browser.storage.onChanged.addListener((changes, areaname) => {
if (areaname === "local" && "state" in changes) {
let mode = changes.state.newValue.mode
const privateMode = browser.extension.inIncognitoContext
? "TridactylPrivate"
: ""
statusIndicator.className =
"cleanslate TridactylStatusIndicator " + privateMode
if (
dom.isTextEditable(document.activeElement) &&
!["input", "ignore"].includes(mode)
) {
statusIndicator.textContent = "insert"
// this doesn't work; statusIndicator.style is full of empty string
// statusIndicator.style.borderColor = "green !important"
// need to fix loss of focus by click: doesn't do anything here.
} else if (
mode === "insert" &&
!dom.isTextEditable(document.activeElement)
) {
statusIndicator.textContent = "normal"
// statusIndicator.style.borderColor = "lightgray !important"
} else {
statusIndicator.textContent = mode
}
addContentStateChangedListener((property, oldValue, newValue) => {
if (property != "mode") {
return
}

let mode = newValue
const privateMode = browser.extension.inIncognitoContext
? "TridactylPrivate"
: ""
statusIndicator.className =
"cleanslate TridactylStatusIndicator " + privateMode
if (
dom.isTextEditable(document.activeElement) &&
!["input", "ignore"].includes(mode)
) {
statusIndicator.textContent = "insert"
// this doesn't work; statusIndicator.style is full of empty string
// statusIndicator.style.borderColor = "green !important"
// need to fix loss of focus by click: doesn't do anything here.
} else if (
mode === "insert" &&
!dom.isTextEditable(document.activeElement)
) {
statusIndicator.textContent = "normal"
// statusIndicator.style.borderColor = "lightgray !important"
} else {
statusIndicator.textContent = mode
}

if (config.get("modeindicator") !== "true") statusIndicator.remove()
})
})
@@ -192,7 +206,7 @@ config.getAsync("leavegithubalone").then(v => {
// On quick loading pages, the document is already loaded
// if (document.location.host == "github.com") {
document.body.addEventListener("keydown", function(e) {
if ("/".indexOf(e.key) != -1 && state.mode == "normal") {
if ("/".indexOf(e.key) != -1 && contentState.mode == "normal") {
e.cancelBubble = true
e.stopImmediatePropagation()
}
@@ -203,7 +217,7 @@ config.getAsync("leavegithubalone").then(v => {
window.addEventListener("DOMContentLoaded", () => {
// if (document.location.host == "github.com") {
document.body.addEventListener("keydown", function(e) {
if ("/".indexOf(e.key) != -1 && state.mode == "normal") {
if ("/".indexOf(e.key) != -1 && contentState.mode == "normal") {
e.cancelBubble = true
e.stopImmediatePropagation()
}

This file was deleted.

@@ -0,0 +1,38 @@
import { parser as exmode_parser } from "./parsers/exmode"
import { repeat } from "./.excmds_background.generated"

import Logger from "./logging"

const logger = new Logger("controller")

export let last_ex_str = ""

/** Parse and execute ExCmds */
// TODO(saulrh): replace this with messaging to the background
export async function acceptExCmd(exstr: string): Promise<any> {
// TODO: Errors should go to CommandLine.
try {
let [func, args] = exmode_parser(exstr)
// Stop the repeat excmd from recursing.
if (func !== repeat) last_ex_str = exstr
try {
return await func(...args)
} catch (e) {
// Errors from func are caught here (e.g. no next tab)
logger.error("background_controller: ", e)
}
} catch (e) {
// Errors from parser caught here
logger.error("background_controller: ", e)
}
}

import * as Messaging from "./messaging"

// Get messages from content
Messaging.addListener(
"controller_background",
Messaging.attributeCaller({
acceptExCmd,
}),
)
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.