@@ -16,25 +16,32 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
1616 chrome . cookies . get ( { url : request . url , name : "sid" , storeId : sender . tab . cookieStoreId } , cookie => {
1717 if ( ! cookie || currentDomain . endsWith ( ".mcas.ms" ) ) { //Domain used by Microsoft Defender for Cloud Apps, where sid exists but cannot be read
1818 sendResponse ( currentDomain ) ;
19- sfHost = currentDomain ;
2019 return ;
2120 }
2221 const [ orgId ] = cookie . value . split ( "!" ) ;
2322 const orderedDomains = [ "salesforce.com" , "cloudforce.com" , "salesforce.mil" , "cloudforce.mil" , "sfcrmproducts.cn" , "force.com" , "salesforce-setup.com" , "visualforce.com" , "sfcrmapps.cn" , "force.mil" , "visualforce.mil" , "crmforce.mil" ] ;
2423
2524 orderedDomains . forEach ( currentDomain => {
2625 chrome . cookies . getAll ( { name : "sid" , domain : currentDomain , secure : true , storeId : sender . tab . cookieStoreId } , cookies => {
27- let sessionCookie = cookies . find ( c => c . value . startsWith ( orgId + "!" ) ) ;
26+ let filteredCookies = cookies . filter ( c => c . value . startsWith ( orgId + "!" ) ) ;
27+ let sessionCookie = null ;
28+ if ( filteredCookies . length > 1 ) {
29+ sessionCookie = filteredCookies . find ( c => c . domain . startsWith ( currentDomain . split ( "." ) [ 0 ] ) ) ;
30+ }
31+ if ( filteredCookies . length === 1 ) {
32+ sessionCookie = filteredCookies [ 0 ] ;
33+ }
2834 if ( sessionCookie ) {
2935 sendResponse ( sessionCookie . domain ) ;
30- sfHost = sessionCookie . domain ;
36+ return ;
3137 }
3238 } ) ;
3339 } ) ;
3440 } ) ;
3541 return true ; // Tell Chrome that we want to call sendResponse asynchronously.
3642 }
3743 if ( request . message == "getSession" ) {
44+ sfHost = request . sfHost ;
3845 chrome . cookies . get ( { url : "https://" + request . sfHost , name : "sid" , storeId : sender . tab . cookieStoreId } , sessionCookie => {
3946 if ( ! sessionCookie ) {
4047 sendResponse ( null ) ;
@@ -45,31 +52,6 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
4552 } ) ;
4653 return true ; // Tell Chrome that we want to call sendResponse asynchronously.
4754 }
48- if ( request . message == "getAllSessions" ) {
49- chrome . cookies . get ( { url : "https://" + request . sfHost , name : "sid" , storeId : sender . tab . cookieStoreId } , cookie => {
50- if ( ! cookie ) { //Domain used by Microsoft Defender for Cloud Apps, where sid exists but cannot be read
51- sendResponse ( null ) ;
52- return ;
53- }
54- const [ orgId ] = cookie . value . split ( "!" ) ;
55- const orderedDomains = [ "salesforce.com" , "cloudforce.com" , "salesforce.mil" , "cloudforce.mil" , "sfcrmproducts.cn" , "force.com" , "salesforce-setup.com" , "visualforce.com" , "sfcrmapps.cn" , "force.mil" , "visualforce.mil" , "crmforce.mil" ] ;
56- orderedDomains . splice ( orderedDomains . indexOf ( request . sfHost ) , 1 ) ;
57- orderedDomains . unshift ( request . sfHost ) ;
58- let cookiesFullList = [ ] ;
59- let i = 0 ;
60- //Promise all do not worked here so just wait for all return with index.
61- orderedDomains . forEach ( currentDomain => chrome . cookies . getAll ( { name : "sid" , domain : currentDomain , secure : true , storeId : sender . tab . cookieStoreId } , cookies => {
62- cookiesFullList . push ( ...cookies ) ;
63- i ++ ;
64- if ( i === orderedDomains . length ) {
65- cookiesFullList = cookiesFullList . filter ( c => c . value . startsWith ( orgId + "!" ) ) ;
66- sendResponse ( cookiesFullList . map ( c => ( { key : c . value , hostname : c . domain } ) ) ) ;
67- }
68- } )
69- ) ;
70- } ) ;
71- return true ; // Tell Chrome that we want to call sendResponse asynchronously.
72- }
7355 if ( request . message == "incognito" ) {
7456 if ( typeof chrome !== "undefined" ) {
7557 chrome . windows . create ( { url : request . url , incognito : true } ) ;
@@ -112,19 +94,19 @@ chrome.runtime.onInstalled.addListener(({reason}) => {
11294 url : "https://dufoli.github.io/Salesforce-Inspector-Advanced/welcome/"
11395 } ) ;
11496 }
115- } ) ;
97+ } ) ;
11698if ( chrome . commands ) {
11799 chrome . commands . onCommand . addListener ( ( command ) => {
118100 //TODO home to open setup
119101 chrome . tabs . create ( {
120102 url : `chrome-extension://${ chrome . i18n . getMessage ( "@@extension_id" ) } /${ command } .html?host=${ sfHost } `
121103 } ) ;
122- } ) ;
104+ } ) ;
123105}
124106if ( chrome . action ) {
125107 chrome . action . onClicked . addListener ( ( ) => {
126108 chrome . tabs . create ( {
127109 url : `chrome-extension://${ chrome . i18n . getMessage ( "@@extension_id" ) } /data-export.html?host=${ sfHost } `
128110 } ) ;
129- } ) ;
111+ } ) ;
130112}
0 commit comments