Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Commit

Permalink
Fix #307, make read intent use content script handlers
Browse files Browse the repository at this point in the history
Also fix #260, 'read' no longer toggles, but only starts reading. Also fix #152, you can stop reading (with 'stop reading') a page that was started manually
  • Loading branch information
ianb committed Sep 27, 2019
1 parent f0b85bf commit 33feb10
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
92 changes: 62 additions & 30 deletions extension/intents/read/read.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
/* globals util, log */
/* globals util, content */

this.intents.read = (function() {
const exports = {};

exports.stopReading = async function() {
const tab = (await browser.tabs.query({ active: true }))[0];
if (!tab) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
if (!activeTab) {
return false;
}
if (!tab.url.startsWith("about:reader")) {
return false;
}
try {
await browser.tabs.sendMessage(tab.id, {
type: "stopReading",
});
return true;
} catch (e) {
log.info("Exception:", String(e), e);
if (!activeTab.url.startsWith("about:reader")) {
return false;
}
await content.lazyInject(activeTab.id, ["/intents/read/startNarration.js"]);
await browser.tabs.sendMessage(activeTab.id, {
type: "stopReading",
});
return true;
};

this.intentRunner.registerIntent({
Expand All @@ -29,27 +25,63 @@ this.intents.read = (function() {
read (this |) (tab | page |)
`,
async run(desc) {
// FIXME: this can fail, we should guard against that and show error:
try {
await browser.tabs.toggleReaderMode();
} catch (e) {
if (
e.message &&
e.message.includes(
"The specified tab cannot be placed into reader mode"
)
) {
e.displayMessage = "This page cannot be put into Reader Mode";
const activeTab = (await browser.tabs.query({ active: true }))[0];
if (!activeTab.url.startsWith("about:reader")) {
try {
await browser.tabs.toggleReaderMode();
} catch (e) {
if (
e.message &&
e.message.includes(
"The specified tab cannot be placed into reader mode"
)
) {
e.displayMessage = "This page cannot be put into Reader Mode";
}
throw e;
}
// FIXME: toggleReaderMode just returns immediately so we have to wait to get this to work
// Ideally it would give an error or something if it was attached to the wrong kind of tab
await util.sleep(1000);
}
await content.lazyInject(activeTab.id, [
"/intents/read/startNarration.js",
]);
const success = await browser.tabs.sendMessage(activeTab.id, {
type: "narrate",
});
if (!success) {
const e = new Error(`Already narrating`);
e.displayMessage = "Already narrating";
throw e;
}
},
});

this.intentRunner.registerIntent({
name: "read.stopRead",
examples: ["Stop reading"],
match: `
stop reading (this |) (tab | page |)
`,
async run(desc) {
const activeTab = (await browser.tabs.query({ active: true }))[0];
if (!activeTab.url.startsWith("about:reader")) {
const e = new Error(`Not a Reader Mode page`);
e.displayMessage = "Page isn't narrating";
throw e;
}
// FIXME: toggleReaderMode just returns immediately so we have to wait to get this to work
// Ideally it would give an error or something if it was attached to the wrong kind of tab
await util.sleep(1000);
await browser.tabs.executeScript({
runAt: "document_end",
file: "/intents/read/startNarration.js",
await content.lazyInject(activeTab.id, [
"/intents/read/startNarration.js",
]);
const success = await browser.tabs.sendMessage(activeTab.id, {
type: "stopReading",
});
if (!success) {
const e = new Error("Not narrating");
e.displayMessage = "Page isn't narrating";
throw e;
}
},
});

Expand Down
35 changes: 17 additions & 18 deletions extension/intents/read/startNarration.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
/* globals communicate */

function startNarration() {
const dropdown = document.querySelector(".narrate-dropdown");
if (dropdown) {
dropdown.classList.add("open");
}
const element = document.querySelector(".narrate-start-stop");
const element = document.querySelector(".narrate-start-stop[title='Start']");
if (!element) {
return false;
}
if (element) {
element.click();
} else {
setTimeout(startNarration, 500);
}
return true;
}

function addListener() {
browser.runtime.onMessage.addListener(message => {
if (message.type === "stopReading") {
const element = document.querySelector(".narrate-start-stop");
if (element) {
// element.click();
console.log("clicking element", element);
element.dispatchEvent(new MouseEvent("click"));
}
} else {
console.log("Received unexpected message in startNarration:", message);
}
});
}

startNarration();
addListener();
communicate.register("narrate", startNarration);
communicate.register("stopReading", () => {
const element = document.querySelector(".narrate-start-stop[title='Stop']");
if (!element) {
return false;
}
console.log("clicking element", element);
element.dispatchEvent(new MouseEvent("click"));
return true;
});

0 comments on commit 33feb10

Please sign in to comment.