@@ -37,7 +37,7 @@ function getPlatformOs() {
37
37
} ) ;
38
38
}
39
39
40
- async function start ( ) {
40
+ async function start ( el ) {
41
41
// All dynamic setting items. Those items will been updated
42
42
// in chrome.storage.onchange listener callback.
43
43
let isOfflineMode = await settings . isOfflineMode ;
@@ -110,7 +110,7 @@ async function start() {
110
110
let rustcSearcher = new RustcSearch ( ) ;
111
111
112
112
const defaultSuggestion = `Search std ${ c . match ( "docs" ) } , external ${ c . match ( "docs" ) } (~,@), ${ c . match ( "crates" ) } (!), ${ c . match ( "attributes" ) } (#), ${ c . match ( "books" ) } (%), clippy ${ c . match ( "lints" ) } (>), and ${ c . match ( "error codes" ) } , etc in your address bar instantly!` ;
113
- const omnibox = new Omnibox ( { defaultSuggestion, maxSuggestionSize : c . omniboxPageSize ( ) } ) ;
113
+ const omnibox = new Omnibox ( { el , defaultSuggestion, maxSuggestionSize : c . omniboxPageSize ( ) } ) ;
114
114
115
115
let formatDoc = ( index , doc ) => {
116
116
let content = doc . href ;
@@ -427,6 +427,40 @@ async function start() {
427
427
428
428
omnibox . addNoCacheQueries ( "/" , "!" , "@" , ":" ) ;
429
429
430
+ // Skip following code if `el` provide, which mean this function run in webpage.
431
+ if ( el ) return ;
432
+
433
+ // DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
434
+ // https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
435
+ chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
436
+ switch ( message . action ) {
437
+ // Rustc:* action is exclusive to rustc docs event
438
+ case "rustc:check" : {
439
+ sendResponse ( {
440
+ version : rustcSearcher . version ,
441
+ } ) ;
442
+ break ;
443
+ }
444
+ case "rustc:add" : {
445
+ if ( message . searchIndex ) {
446
+ rustcSearcher . setSearchIndex ( message . searchIndex ) ;
447
+ rustcSearcher . setVersion ( message . version ) ;
448
+ sendResponse ( true ) ;
449
+ } else {
450
+ sendResponse ( false ) ;
451
+ }
452
+ break ;
453
+ }
454
+ case "open-url" : {
455
+ if ( message . url ) {
456
+ Omnibox . navigateToUrl ( message . url ) ;
457
+ }
458
+ break ;
459
+ }
460
+ }
461
+ return true ;
462
+ } ) ;
463
+
430
464
chrome . storage . onChanged . addListener ( changes => {
431
465
for ( let [ key , { oldValue, newValue } ] of Object . entries ( changes ) ) {
432
466
console . log ( 'storage key updated:' , key ) ;
@@ -520,6 +554,10 @@ async function start() {
520
554
}
521
555
} ) ;
522
556
557
+ await checkAutoUpdate ( ) ;
558
+ }
559
+
560
+ async function checkAutoUpdate ( ) {
523
561
if ( await settings . autoUpdate ) {
524
562
let version = await storage . getItem ( 'auto-update-version' ) ;
525
563
let now = new Date ( ) ;
@@ -532,37 +570,6 @@ async function start() {
532
570
Omnibox . navigateToUrl ( INDEX_UPDATE_URL ) ;
533
571
await storage . setItem ( 'auto-update-version' , `${ now . getFullYear ( ) } -${ now . getMonth ( ) + 1 } -${ now . getDate ( ) } ` ) ;
534
572
}
535
-
536
- // DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE:
537
- // https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049
538
- chrome . runtime . onMessage . addListener ( ( message , sender , sendResponse ) => {
539
- switch ( message . action ) {
540
- // Rustc:* action is exclusive to rustc docs event
541
- case "rustc:check" : {
542
- sendResponse ( {
543
- version : rustcSearcher . version ,
544
- } ) ;
545
- break ;
546
- }
547
- case "rustc:add" : {
548
- if ( message . searchIndex ) {
549
- rustcSearcher . setSearchIndex ( message . searchIndex ) ;
550
- rustcSearcher . setVersion ( message . version ) ;
551
- sendResponse ( true ) ;
552
- } else {
553
- sendResponse ( false ) ;
554
- }
555
- break ;
556
- }
557
- case "open-url" : {
558
- if ( message . url ) {
559
- Omnibox . navigateToUrl ( message . url ) ;
560
- }
561
- break ;
562
- }
563
- }
564
- return true ;
565
- } ) ;
566
573
}
567
574
568
575
const chromeAction = chrome . action || chrome . browserAction ;
@@ -578,4 +585,5 @@ chrome.runtime.onInstalled.addListener(async ({ previousVersion, reason }) => {
578
585
}
579
586
} ) ;
580
587
588
+
581
589
export { start } ;
0 commit comments