Skip to content

Commit

Permalink
fix GoogleTranslate
Browse files Browse the repository at this point in the history
  • Loading branch information
ken107 committed Aug 14, 2021
1 parent db34f9e commit 6489c40
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
"error_wavenet_auth_required": {
"message": "You need to grant additional [permissions](auth-wavenet) to enable Google Wavenet voices"
},
"error_gtranslate_auth_required": {
"message": "You need to grant additional [permissions](auth-gtranslate) to enable Google Translate voices"
},
"error_user_gesture_required": {
"message": "Click [here](user-gesture) to start"
},
Expand Down
4 changes: 4 additions & 0 deletions js/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ var config = {
permissions: ["webRequest"],
origins: ["https://*/"]
},
gtranslatePerms: {
permissions: ["webRequest", "webRequestBlocking"],
origins: ["https://translate.google.com/*"]
},
}

var defaults = {
Expand Down
24 changes: 24 additions & 0 deletions js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ silenceLoop.loop = true;
brapi.runtime.onInstalled.addListener(installContextMenus);
if (getBrowser() == "firefox") brapi.runtime.onStartup.addListener(installContextMenus);

hasPermissions(config.gtranslatePerms)
.then(function(granted) {
if (granted) authGoogleTranslate()
})


/**
* IPC handlers
Expand Down Expand Up @@ -289,6 +294,25 @@ function authWavenet() {
})
}

function authGoogleTranslate() {
console.info("Installing GoogleTranslate XHR hook")
brapi.webRequest.onBeforeSendHeaders.removeListener(googleTranslateXhrHook)
brapi.webRequest.onBeforeSendHeaders.addListener(googleTranslateXhrHook, {
urls: config.gtranslatePerms.origins,
types: ["xmlhttprequest"]
}, [
"blocking", "requestHeaders"
])
}

function googleTranslateXhrHook(details) {
var header = details.requestHeaders.find(function(h) {return h.name == "Sec-Fetch-Site"})
if (header && header.value == "cross-site") header.value = "none"
return {
requestHeaders: details.requestHeaders
}
}

function userGestureActivate() {
var audio = document.createElement("AUDIO");
audio.src = "data:audio/wav;base64,UklGRjIAAABXQVZFZm10IBIAAAABAAEAQB8AAEAfAAABAAgAAABmYWN0BAAAAAAAAABkYXRhAAAAAA==";
Expand Down
1 change: 1 addition & 0 deletions js/firefox-perm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ $(function() {
if (granted) {
$("#success").show();
if (query.then == "auth-wavenet") getBackgroundPage().then(callMethod("authWavenet")).then(closeThisTab);
else if (query.then == "auth-gtranslate") getBackgroundPage().then(callMethod("authGoogleTranslate"))
}
else $("#error").show();
})
Expand Down
14 changes: 12 additions & 2 deletions js/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,25 @@ function handleError(err) {
break;
case "#auth-wavenet":
if (getBrowser() == "firefox") {
createTab(brapi.runtime.getURL("firefox-perm.html") + "?perms=" + encodeURIComponent(JSON.stringify(config.wavenetPerms)) + "&then=auth-wavenet");
window.close();
createTab(brapi.runtime.getURL("firefox-perm.html") + "?perms=" + encodeURIComponent(JSON.stringify(config.wavenetPerms)) + "&then=auth-wavenet")
.then(function() {
window.close()
})
break;
}
requestPermissions(config.wavenetPerms)
.then(function(granted) {
if (granted) bgPageInvoke("authWavenet");
})
break;
case "#auth-gtranslate":
if (getBrowser() == "firefox") {
createTab(brapi.runtime.getURL("firefox-perm.html") + "?perms=" + encodeURIComponent(JSON.stringify(config.gtranslatePerms)) + "&then=auth-gtranslate")
.then(function() {
window.close()
})
}
break;
case "#user-gesture":
getBackgroundPage()
.then(callMethod("userGestureActivate"))
Expand Down
1 change: 1 addition & 0 deletions js/speech.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function Speech(texts, options) {
return googleTranslateTtsEngine.ready()
.then(function() {return googleTranslateTtsEngine})
.catch(function(err) {
if (/^{/.test(err.message)) throw err
console.error(err);
options.voice.autoSelect = true;
return remoteTtsEngine;
Expand Down
6 changes: 5 additions & 1 deletion js/tts-engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,11 @@ function GoogleTranslateTtsEngine() {
var isSpeaking = false;
var speakPromise;
this.ready = function() {
return googleTranslateReady();
return hasPermissions(config.gtranslatePerms)
.then(function(granted) {
if (!granted) throw new Error(JSON.stringify({code: "error_gtranslate_auth_required"}))
})
.then(googleTranslateReady)
};
this.speak = function(utterance, options, onEvent) {
if (!options.volume) options.volume = 1;
Expand Down
1 change: 1 addition & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
],
"optional_permissions": [
"webRequest",
"webRequestBlocking",
"webNavigation",
"http://*/",
"https://*/",
Expand Down

0 comments on commit 6489c40

Please sign in to comment.