This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function initMessageHandler() { | |
// ... | |
useEffect(() => { | |
// Listening for updates by the service worker | |
chrome.runtime.onMessage.addListener(handleMessageListener); | |
return () => { | |
chrome.runtime.onMessage.removeListener(handleMessageListener); | |
}; | |
}, []); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function updateLanguage(language: LanguagesSupported) { | |
chrome.storage.local.set({ language }); | |
broadcastMessageAllTabs({ | |
type: "languageUpdated", | |
data: { | |
language: language, | |
translations: i18n.getDataByLanguage(language), | |
}, | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onAuthSuccess(user: User) { | |
// Initialise your client environment after authentication | |
//... | |
setLanguageCookie(user.language) | |
i18n.changeLanguage(user.language) | |
} | |
// ... | |
function onUserLanguageChange(language: LanguagesSupported) { | |
// ... | |
setLanguageCookie(language) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Message and response are define in https://gist.github.com/EduardoAC/000b1e39a6ec10a892e7c6cd93730a53 | |
export function broadcastMessageAllLoadedTabs<T>(message: Message<T>) { | |
// Get all tabs not discarded - it can be optimise further | |
chrome.tabs.query({ discarded: false }, (tabs) => { | |
tabs.forEach(({ id, status }) => { | |
if(status !== "unloaded") { | |
sendMessage(id, message) | |
} | |
}) | |
}) |
NewerOlder