Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] - Fix iPad Translate with Page History Navigation #27892

Merged
merged 2 commits into from
Mar 3, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class BraveTranslateTabHelper: NSObject {
self.url = url
self.isTranslationReady = false
self.canShowToast = false
self.currentLanguageInfo = BraveTranslateLanguageInfo()
self.translationTask = nil
self.delegate?.updateTranslateURLBar(tab: self.tab, state: .unavailable)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ extension BrowserViewController: BraveTranslateScriptHandlerDelegate {
// Ensure url bar is expanded before presenting a popover on it
toolbarVisibilityViewModel.toolbarState = .expanded

DispatchQueue.main.async {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

yikes

let popover = PopoverController(
content: OnboardingTranslateView(
onContinueButtonPressed: { [weak self, weak tab] in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,27 @@ Object.defineProperty(window.__firefox__, "$<brave_translate_script>", {
"getRawPageSource": (function() {
return document.documentElement.outerText;
}),
"hasNoTranslate": (function() {
for (const metaTag of document.getElementsByTagName('meta')) {
if (metaTag.name === 'google') {
if (metaTag.content === 'notranslate' ||
metaTag.getAttribute('value') === 'notranslate') {
return true;
}
}
}
return false;
}),
"isSameLanguage": (function() {
let userLanguage = (navigator.language || navigator.userLanguage).split('-')[0];
let pageLanguage = document.documentElement.lang
return userLanguage == pageLanguage;
}),
"checkTranslate": (function() {
try {
if ((cr.googleTranslate.libReady || cr.googleTranslate.finished) && !cr.googleTranslate.readyCallback) {
if ((cr.googleTranslate.libReady || cr.googleTranslate.finished) &&
!cr.googleTranslate.readyCallback &&
!window.__firefox__.$<brave_translate_script>.isSameLanguage()) {
window.webkit.messageHandlers["$<message_handler>"].postMessage({
"command": "ready"
});
Expand Down Expand Up @@ -133,10 +151,10 @@ try {
}
};

const emptySvgDataUrl = 'data:image/svg+xml;base64,' + btoa('<svg xmlns="http://www.w3.org/2000/svg"/>');

// Make replacements in loading .js files.
function processJavascript(text) {
const emptySvgDataUrl = 'data:image/svg+xml;base64,' + btoa('<svg xmlns="http://www.w3.org/2000/svg"/>');

// Replace gen204 telemetry requests with loading an empty svg.
text = text.replaceAll('"//"+po+"/gen204?"+Bo(b)', '"' + emptySvgDataUrl + '"');

Expand All @@ -148,6 +166,8 @@ try {

// Make replacements in loading .css files.
function processCSS(text) {
const emptySvgDataUrl = 'data:image/svg+xml;base64,' + btoa('<svg xmlns="http://www.w3.org/2000/svg"/>');

// Used in the injected elements, that are currently not visible. Replace it
// to hide the loading error in devtools (because of CSP).
text = text.replaceAll('//www.gstatic.com/images/branding/product/2x/translate_24dp.png', emptySvgDataUrl);
Expand Down Expand Up @@ -291,17 +311,17 @@ try {
xhr.send();
}

// The styles to hide root elements that are injected by the scripts in the DOM.
// Currently they are always invisible. The styles are added in case of changes
// in future versions.
const braveExtraStyles = `.goog-te-spinner-pos, #goog-gt-tt {display: none;}`

// An overridden version of onLoadCSS from translate.js.
// The differences:
// 1. change url via rewriteUrl();
// 2. process the loaded styles via processCSS().
// 3. Add braveExtraStyles in the end.
cr.googleTranslate.onLoadCSS = function(url) {
// The styles to hide root elements that are injected by the scripts in the DOM.
// Currently they are always invisible. The styles are added in case of changes
// in future versions.
const braveExtraStyles = `.goog-te-spinner-pos, #goog-gt-tt {display: none;}`

const xhr = new XMLHttpRequest();
xhr.open('GET', rewriteUrl(url), true);
xhr.onreadystatechange = function() {
Expand Down
Loading