1+ import 'chrome-extension-async'
2+ import { getActiveTab , setLastFocusedWindowId } from 'libs'
3+
14const getURL = ( { url } ) => new window . URL ( url )
25
36const reloadTab = ( tab ) => chrome . tabs . reload ( tab . id )
47
58const methodMap = {
6- getData : ( message , { url } , sendResponse ) => {
9+ getData : async ( message , { url } , sendResponse ) => {
710 const { host, protocol, origin } = url
8- chrome . storage . sync . get ( origin , ( data ) => {
9- const customjs = data [ origin ]
10- sendResponse ( { customjs, host, protocol } )
11- } )
11+ const data = await chrome . store . sync . get ( origin )
12+ const customjs = data [ origin ]
13+ sendResponse ( { customjs, host, protocol } )
1214 } ,
1315 setData : ( message , { url } ) => chrome . storage . sync . set (
1416 { [ url . origin ] : message . customjs }
@@ -17,32 +19,34 @@ const methodMap = {
1719 goTo : ( message , { tab } ) => chrome . tabs . update ( tab . id , { url : message . link } )
1820}
1921
20- const onMessage = ( message , sender , sendResponse ) => chrome . tabs . query (
21- { active : true , currentWindow : true } ,
22- ( tabs ) => {
23- if ( tabs . length <= 0 ) {
24- // TODO: handle this in UI
25- throw new Error ( 'No active tab! This is impossible' )
26- }
27- const [ tab ] = tabs
28- const url = getURL ( tab )
29- const { method, reload } = message
30-
31- const func = methodMap [ method ]
32- if ( func && typeof func === 'function' ) {
33- func ( message , { tab, url } , sendResponse )
34- } else {
35- console . error ( `Unknown method: ${ method } ` )
36- sendResponse ( { src : '' , config : { } } )
37- }
38-
39- if ( reload ) {
40- reloadTab ( tab )
41- }
22+ const onMessage = async ( message , sender , sendResponse ) => {
23+ const tab = await getActiveTab ( )
24+ const url = getURL ( tab )
25+ const { method, reload } = message
26+
27+ const func = methodMap [ method ]
28+ if ( func && typeof func === 'function' ) {
29+ func ( message , { tab, url } , sendResponse )
30+ } else {
31+ console . error ( `Unknown method: ${ method } ` )
32+ sendResponse ( { src : '' , config : { } } )
4233 }
43- )
34+
35+ if ( reload ) {
36+ reloadTab ( tab )
37+ }
38+ }
39+
40+ const onFocusChanged = ( windowId ) => {
41+ if ( windowId < 0 ) {
42+ return
43+ }
44+ setLastFocusedWindowId ( windowId )
45+ }
4446
4547chrome . runtime . onMessage . addListener ( ( ...args ) => {
4648 onMessage ( ...args )
4749 return true
4850} )
51+
52+ chrome . windows . onFocusChanged . addListener ( onFocusChanged )
0 commit comments