Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,42 @@ try {
"js/defaults.js",
"js/messaging.js",
"js/content-handlers.js",
"js/events.js"
"js/events.js",
"js/download.js"
)
}
catch (err) {
} catch (err) {
console.error(err)
}

registerMessageListener("background", {
async downloadSelectedText(text) {
console.log("BG: synthesizing (no player)", text);

const settings = await getSettings(["voiceName", "rate", "pitch", "voices"]);
const voice = (settings.voices || [])[0]; // fallback

const options = {
voice,
lang: voice.lang,
rate: settings.rate,
pitch: settings.pitch
};

// Create speech instance WITHOUT player
const speech = new Speech([text], options);

// Use engine directly
const engine =
speech.engine ||
speech._engine ||
speech.__engine;

const url = await engine.getAudioUrl(text, options.voice, options.pitch);

const blob = await fetch(url).then(r => r.blob());

return await blobToBase64(blob);
}
});


52 changes: 26 additions & 26 deletions js/content.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

(function() {
(function () {
registerMessageListener("contentScript", {
getRequireJs: getRequireJs,
getDocumentInfo: getInfo,
getCurrentIndex: getCurrentIndex,
getTexts: getTexts
getTexts: getTexts,
})

function getInfo() {
Expand All @@ -17,7 +17,7 @@

function getLang() {
var lang = document.documentElement.lang || $("html").attr("xml:lang");
if (lang) lang = lang.split(",",1)[0].replace(/_/g, '-');
if (lang) lang = lang.split(",", 1)[0].replace(/_/g, '-');
return lang;
}

Expand Down Expand Up @@ -47,9 +47,9 @@
|| $("embed[type='application/pdf']").length
|| $("iframe[src*='.pdf']").length) return ["js/content/pdf-doc.js"];
else if (/^\d+\.\d+\.\d+\.\d+$/.test(location.hostname)
&& location.port === "1122"
&& location.protocol === "http:"
&& location.pathname === "/bookshelf/index.html") return ["js/content/yd-app-web.js"];
&& location.port === "1122"
&& location.protocol === "http:"
&& location.pathname === "/bookshelf/index.html") return ["js/content/yd-app-web.js"];
else return ["js/content/html-doc.js"];
}

Expand All @@ -65,7 +65,7 @@
}
else {
return Promise.resolve(readAloudDoc.getTexts(index, quietly))
.then(function(texts) {
.then(function (texts) {
if (texts && Array.isArray(texts)) {
if (!quietly) console.log(texts.join("\n\n"));
}
Expand All @@ -90,7 +90,7 @@
if (!audioCanPlay()) return;
const silenceTrack = getSilenceTrack()
try {
const should = await sendToPlayer({method: "shouldPlaySilence", args: [providerId]})
const should = await sendToPlayer({ method: "shouldPlaySilence", args: [providerId] })
if (should) silenceTrack.start()
else silenceTrack.stop()
}
Expand Down Expand Up @@ -128,7 +128,7 @@ function isNotEmpty(text) {
function fixParagraphs(texts) {
var out = [];
var para = "";
for (var i=0; i<texts.length; i++) {
for (var i = 0; i < texts.length; i++) {
if (!texts[i]) {
if (para) {
out.push(para);
Expand All @@ -137,7 +137,7 @@ function fixParagraphs(texts) {
continue;
}
if (para) {
if (/[-\u2013\u2014]$/.test(para)) para = para.substr(0, para.length-1);
if (/[-\u2013\u2014]$/.test(para)) para = para.substr(0, para.length - 1);
else para += " ";
}
para += texts[i].replace(/[-\u2013\u2014]\r?\n/g, "");
Expand All @@ -153,8 +153,8 @@ function fixParagraphs(texts) {
function tryGetTexts(getTexts, millis) {
return waitMillis(500)
.then(getTexts)
.then(function(texts) {
if (texts && !texts.length && millis-500 > 0) return tryGetTexts(getTexts, millis-500);
.then(function (texts) {
if (texts && !texts.length && millis - 500 > 0) return tryGetTexts(getTexts, millis - 500);
else return texts;
})
}
Expand All @@ -181,20 +181,20 @@ function simulateMouseEvent(element, eventName, coordX, coordY) {

function simulateClick(elementToClick) {
var box = elementToClick.getBoundingClientRect(),
coordX = box.left + (box.right - box.left) / 2,
coordY = box.top + (box.bottom - box.top) / 2;
simulateMouseEvent (elementToClick, "mousedown", coordX, coordY);
simulateMouseEvent (elementToClick, "mouseup", coordX, coordY);
simulateMouseEvent (elementToClick, "click", coordX, coordY);
coordX = box.left + (box.right - box.left) / 2,
coordY = box.top + (box.bottom - box.top) / 2;
simulateMouseEvent(elementToClick, "mousedown", coordX, coordY);
simulateMouseEvent(elementToClick, "mouseup", coordX, coordY);
simulateMouseEvent(elementToClick, "click", coordX, coordY);
}

const getMath = (function() {
const getMath = (function () {
let promise = Promise.resolve(null)
return () => promise = promise.then(math => math || makeMath())
})();

async function makeMath() {
const getXmlFromMathEl = function(mathEl) {
const getXmlFromMathEl = function (mathEl) {
const clone = mathEl.cloneNode(true)
$("annotation, annotation-xml", clone).remove()
removeAllAttrs(clone, true)
Expand All @@ -210,11 +210,11 @@ async function makeMath() {
return mathEl ? getXmlFromMathEl(mathEl) : el.getAttribute("data-mathml")
},
})
.when(() => document.querySelector("math"), {
selector: "math",
getXML: getXmlFromMathEl,
})
.else(null)
.when(() => document.querySelector("math"), {
selector: "math",
getXML: getXmlFromMathEl,
})
.else(null)

if (!math) return null
const elems = $(math.selector).get()
Expand All @@ -229,8 +229,8 @@ async function makeMath() {
catch (err) {
console.error(err)
return {
show() {},
hide() {}
show() { },
hide() { }
}
}

Expand Down
Loading