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

Always run initialization code on browser start #295

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions extension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ async function start(omnibox) {

if (!omnibox.extensionMode) return;

// NOTE: this kind of async registration is specifically discouraged in manifest v2
// not sure if it works properly in v3? https://developer.chrome.com/docs/extensions/mv2/background-pages#listeners
// See also https://developer.chrome.com/docs/extensions/develop/migrate/to-service-workers#register-listeners
chrome.storage.onChanged.addListener(async changes => {
for (let [key, { oldValue, newValue }] of Object.entries(changes)) {
console.log('storage key updated:', key);
Expand Down
7 changes: 5 additions & 2 deletions extension/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
return true;
});

(async () => {
async function init() {
const defaultSuggestion = `Search std <match>docs</match>, external <match>docs</match> (~,@), <match>crates</match> (!), <match>attributes</match> (#), <match>books</match> (%), clippy <match>lints</match> (>), and <match>error codes</match>, etc in your address bar instantly!`;
const omnibox = Omnibox.extension({ defaultSuggestion, maxSuggestionSize: Compat.omniboxPageSize() });
await start(omnibox);
await checkAutoUpdate();
})();
}

chrome.runtime.onStartup.addListener(init);
chrome.runtime.onInstalled.addListener(init);