Skip to content
This repository was archived by the owner on Feb 26, 2021. It is now read-only.
9 changes: 8 additions & 1 deletion extension/background/entityTypes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { languageNames } from "./languages.js";
import { metadata } from "../services/metadata.js";
import { convertEntities } from "../language/compiler.js";
import { convertEntities, makeWordMatcher } from "../language/compiler.js";
import English from "../language/langs/english.js";

export const allServiceNames = [];
Expand All @@ -21,9 +21,16 @@ for (const id in metadata.music) {
}
}

export const pageName = [];

export function addPageName(name) {
entityTypes.pageName.alternatives.push(makeWordMatcher(name));
}

export const entityTypes = convertEntities({
serviceName: allServiceNames,
musicServiceName: musicServiceNames,
pageName,
lang: languageNames(),
smallNumber: English.numberNames,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

convertEntities does its own thing. It might actually be OK, but then if you look at entityTypes.name (it would be better to call it .pageName) you'll see it is an instance of Alternatives.

A good approach here is to use things like console.log(entityTypes.name) to see what's what.

But to add a new thing to the entity you'd do something like:

export function addPageName(name) {
  entityTypes.pageNames.alternatives.push(makeWordMatcher(name));
}

(Note makeWordMatcher isn't exported at the moment, but could be)

28 changes: 11 additions & 17 deletions extension/background/intentRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { metadata } from "../intents/metadata.js";
import { compile, splitPhraseLines } from "../language/compiler.js";
import { PhraseSet } from "../language/findMatch.js";
import * as settings from "../settings.js";
import { entityTypes } from "./entityTypes.js";
import { entityTypes, addPageName } from "./entityTypes.js";
import * as intentParser from "./intentParser.js";
import * as telemetry from "./telemetry.js";
import { registerHandler, sendMessage } from "../communicate.js";
Expand Down Expand Up @@ -548,33 +548,27 @@ export function getRegisteredRoutines() {

registerHandler("getRegisteredRoutines", getRegisteredRoutines);

let pageNames = {};
export async function getRegisteredPageName() {
const result = await browser.storage.sync.get("pageNames");
const pageNames = result.pageNames;

return pageNames;
}

export async function registerPageName(name, { url }) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is the function where I am to save the pageNames to the entityTypes, it's not clear to me how the services communicate

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this should call a new function in entityTypes.js

name = name.toLowerCase();
const creationDate = Date.now();
const result = await browser.storage.sync.get("pageNames");
pageNames = result.pageNames || {};
const pageNames = (await getRegisteredPageName()) || {};

pageNames[name] = url;

log.info("Added routine for page", name, "->", url, creationDate);
addPageName(name);
await browser.storage.sync.set({ pageNames });
}

export async function getRegisteredPageName(name) {
const result = await browser.storage.sync.get("pageNames");
pageNames = result.pageNames;

if (!pageNames[name] || !name) {
const exc = new Error("No page name to remove");
exc.displayMessage = `The page name "${name}" not found`;
throw exc;
}
return pageNames;
log.info("Added routine for page", name, "->", url, creationDate);
}

export async function unregisterPageName(name) {
const pageNames = await getRegisteredPageName();
delete pageNames[name];
log.info("Removed routine for page", name);
await browser.storage.sync.set({ pageNames });
Expand Down
5 changes: 2 additions & 3 deletions extension/intents/navigation/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ intentRunner.registerIntent({
name: "navigation.navigate",
async run(context) {
const query = context.slots.query.toLowerCase();
const result = await browser.storage.sync.get("pageNames");
const pageNames = result.pageNames;
const pageNames = await intentRunner.getRegisteredPageName();
let tab = null;
if (pageNames && pageNames[query]) {
const savedUrl = pageNames[query];
tab = await browserUtil.openOrFocusTab(savedUrl);
} else {
const where = context.slots.where;
const cached = queryDatabase.get(query.toLowerCase());
const cached = queryDatabase.get(query);
if (where === "window") {
if (cached) {
const window = await browser.windows.create({ url: cached.url });
Expand Down
10 changes: 8 additions & 2 deletions extension/intents/routines/routines.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import * as intentRunner from "../../background/intentRunner.js";
import * as pageMetadata from "../../background/pageMetadata.js";
import * as browserUtil from "../../browserUtil.js";
import { RoutineExecutor, pausedRoutineExecutor } from "./routineExecutor.js";
import English from "../../language/langs/english.js";
import { pausedRoutineExecutor, RoutineExecutor } from "./routineExecutor.js";

intentRunner.registerIntent({
name: "routines.name",
Expand Down Expand Up @@ -98,7 +98,13 @@ intentRunner.registerIntent({
name: "routines.removePageName",
async run(context) {
const name = context.slots.name;
await intentRunner.getRegisteredPageName(name);
const pageNames = await intentRunner.getRegisteredPageName();
if (!pageNames[name] || !name) {
const exc = new Error(`No page name with name ${name}`);
exc.displayMessage = `The page name "${name}" not found`;
throw exc;
}

intentRunner.unregisterPageName(name);
},
});
Expand Down
2 changes: 1 addition & 1 deletion extension/language/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function convertEntities(entityMapping) {
return result;
}

function makeWordMatcher(string) {
export function makeWordMatcher(string) {
const list = makeWordList(string);
if (list.length === 1) {
return list[0];
Expand Down