Skip to content

Commit

Permalink
Move commands to data file
Browse files Browse the repository at this point in the history
  • Loading branch information
hao1300 committed Jun 7, 2020
1 parent 4b62584 commit 5856b07
Show file tree
Hide file tree
Showing 12 changed files with 1,697 additions and 1,317 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ typings/

build
dist
.idea
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"html-webpack-plugin": "3.2.0",
"mini-css-extract-plugin": "^0.9.0",
"node-sass": "^4.14.0",
"prettier": "^2.0.5",
"sass-loader": "^8.0.0",
"style-loader": "^0.23.1",
"svelte": "^3.21.0",
Expand Down
255 changes: 130 additions & 125 deletions src/js/commander.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,130 @@
import { annyang } from "./annyang";
import { DEBUG } from "./common";
import { sendMessage, resendMessageIfAvailable } from "./notification";
import { activeListening, isActiveListening } from './store';
import { getActiveTab, performActionWithDelay } from './core';

const DEFAULT_COMMAND_PRIORITY = 0.5;
const addedCommands = new Set();

/** ------- Initialization ------- */
export function initCommander(allPlugins) {
initRegularCommands(allPlugins);

chrome.runtime.onMessage.addListener(async (request) => {
if (DEBUG) {
console.log(`Received message: ${JSON.stringify(request)}`);
}
switch (request.type) {
case "TRIGGER":
trigger();
break;
case "OPEN_URL":
chrome.tabs.create({
url: request.url
});
break;
case "QUERY":
annyang.trigger(request.query);
break;
case "CLEAR_NOTIFICATION":
activeListening.set(false);
break;
case "TAB_LOADED":
resendMessageIfAvailable();
break;
}
});

annyang.addCallback("resultMatch", async userSaid => {
sendResultMessage(userSaid);
autoCloseIfNeeded();
});

annyang.addCallback("result", results => {
const result = results[0];
if (result && isActiveListening()) {
sendMessage({
type: "PENDING_RESULT",
title: "Listening",
content: result
});
}
});
annyang.addCallback("hotwordTrigger", () => {
trigger();
});
}

function sendResultMessage(userSaid) {
if (isActiveListening()) {
sendMessage({
type: "RESULT",
title: "Received command",
content: userSaid
});
}
}

/** ------- Helper functions to perform actions ------- */
async function trigger() {
const activeTab = await getActiveTab();
if (!activeTab.url || activeTab.url.startsWith("chrome")) {
// We can't use content script on chrome URLs, so need to create a new tab.
chrome.tabs.create({ url: "https://www.google.com" }, () => {
activeListening.set(true);
});
} else {
activeListening.set(true);
}
}

function addCommands(
commandList,
commandFunction,
{ priority = DEFAULT_COMMAND_PRIORITY } = {}
) {
for (const cmd of commandList) {
const command = cmd.toLowerCase();
if (DEBUG) {
if (command.trim() != command) {
console.error(`${command} is not trimmed`);
}
if (addedCommands.has(command)) {
console.error(`${command} has already been added`);
}
addedCommands.add(command);
}
annyang.addCommands({ [command]: commandFunction }, priority)
}
}

/** ------- Handle regular commands ------- */
function initRegularCommands(allPlugins) {
for (const command of allPlugins) {
if (DEBUG) {
const keys = Object.keys(command);
if (!keys.includes("commands") || !keys.includes("callback")) {
console.error(`Invalid command in plugin: `, command);
}
}
addCommands(command.commands, command.callback, {
priority: command.priority || DEFAULT_COMMAND_PRIORITY
});
}
}

function autoCloseIfNeeded() {
performActionWithDelay(() => {
chrome.storage.local.get(["autoOff"], result => {
if (result.autoOff) {
activeListening.set(false);
}
});
})
}
import { annyang } from "./annyang";
import { DEBUG } from "./common";
import { sendMessage, resendMessageIfAvailable } from "./notification";
import { activeListening, isActiveListening } from './store';
import { getActiveTab, performActionWithDelay } from './core';
import enData from './langs/en.json';

const DEFAULT_COMMAND_PRIORITY = 0.5;
const addedCommands = new Set();

/** ------- Initialization ------- */
export function initCommander(allPlugins) {
initRegularCommands(allPlugins);

chrome.runtime.onMessage.addListener(async (request) => {
if (DEBUG) {
console.log(`Received message: ${JSON.stringify(request)}`);
}
switch (request.type) {
case "TRIGGER":
trigger();
break;
case "OPEN_URL":
chrome.tabs.create({
url: request.url
});
break;
case "QUERY":
annyang.trigger(request.query);
break;
case "CLEAR_NOTIFICATION":
activeListening.set(false);
break;
case "TAB_LOADED":
resendMessageIfAvailable();
break;
}
});

annyang.addCallback("resultMatch", async userSaid => {
sendResultMessage(userSaid);
autoCloseIfNeeded();
});

annyang.addCallback("result", results => {
const result = results[0];
if (result && isActiveListening()) {
sendMessage({
type: "PENDING_RESULT",
title: "Listening",
content: result
});
}
});
annyang.addCallback("hotwordTrigger", () => {
trigger();
});
}

function sendResultMessage(userSaid) {
if (isActiveListening()) {
sendMessage({
type: "RESULT",
title: "Received command",
content: userSaid
});
}
}

/** ------- Helper functions to perform actions ------- */
async function trigger() {
const activeTab = await getActiveTab();
if (!activeTab.url || activeTab.url.startsWith("chrome")) {
// We can't use content script on chrome URLs, so need to create a new tab.
chrome.tabs.create({ url: "https://www.google.com" }, () => {
activeListening.set(true);
});
} else {
activeListening.set(true);
}
}

function addCommands(
commandList,
commandFunction,
{ priority = DEFAULT_COMMAND_PRIORITY } = {}
) {
for (const cmd of commandList) {
const command = cmd.toLowerCase();
if (DEBUG) {
if (command.trim() !== command) {
console.error(`${command} is not trimmed`);
}
if (addedCommands.has(command)) {
console.error(`${command} has already been added`);
}
addedCommands.add(command);
}
annyang.addCommands({ [command]: commandFunction }, priority)
}
}

/** ------- Handle regular commands ------- */
function initRegularCommands(allPlugins) {
const actionToCommand = {};
for (const command of allPlugins) {
if (DEBUG) {
const keys = Object.keys(command);
if (!keys.includes("action") || keys.includes('commands') || !keys.includes("callback")) {
console.error(`Invalid command in plugin: `, command);
}
}
actionToCommand[command.action] = command;
}
for (const [action, commands] of Object.entries(enData)) {
addCommands(commands, actionToCommand[action].callback, {
priority: actionToCommand[action].priority || DEFAULT_COMMAND_PRIORITY
});
}
}

function autoCloseIfNeeded() {
performActionWithDelay(() => {
chrome.storage.local.get(["autoOff"], result => {
if (result.autoOff) {
activeListening.set(false);
}
});
})
}
Loading

0 comments on commit 5856b07

Please sign in to comment.