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

Update button detection on new Chrome webstore design (uplift to 1.58.x) #20191

Merged
merged 1 commit into from
Sep 19, 2023
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 @@ -44,7 +44,8 @@
},
{
"matches": [
"https://chrome.google.com/webstore/*"
"https://chrome.google.com/webstore/*",
"https://chromewebstore.google.com/*"
],
"js": [
"out/webstore.bundle.js"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { getLocale } from './background/api/localeAPI'

const config = { attributes: true, childList: true, subtree: true }

const callback = (mutationsList: MutationRecord[], observer: MutationObserver) => {
const buttons = document.querySelectorAll('div.webstore-test-button-label')
buttons.forEach((button: Element) => {
const callback = () => {
const buttonQueries = [
'div.webstore-test-button-label', // https://chrome.google.com/webstore
'button span[jsname]:not(:empty)' // https://chromewebstore.google.com/
]
for (const button of document.querySelectorAll(buttonQueries.join(','))) {
const text = button.textContent || ''
if (text === getLocale('addToChrome')) {
button.textContent =
Expand All @@ -18,11 +21,14 @@ const callback = (mutationsList: MutationRecord[], observer: MutationObserver) =
button.textContent =
getLocale('removeFromBrave') || text.replace('Chrome', 'Brave')
}
})
}
}

const observer: MutationObserver = new MutationObserver(callback)

window.onload = () => {
observer.observe(document, config)
}
// At https://chromewebstore.google.com, "*Chrome" buttons are displayed
// shortly after the first paint, and long before either `DOMContentLoaded` or
// `window.load` fire, so we must start watching for document mutations as soon
// as possible. The performance impact of observing mutations prior to `onload`
// is non-trivial but small (measured at 60ms on a development build).
observer.observe(document, config)