@@ -43,6 +43,9 @@ local currentRecordUri = "";
4343
4444local gridColumns = {};
4545
46+ local performedAutoSearch = false ;
47+ local transactionNumber = 0 ;
48+
4649local archiveSpaceAddonScript = [[
4750 function buildObjectUrl(currentTreeId) {
4851 var archivalObjectId = /archival_object_(\d+)/.exec(currentTreeId);
@@ -139,25 +142,17 @@ function Init()
139142 -- After we add all of our buttons and form elements, we can show the form.
140143 catalogSearchForm .Form :Show ();
141144 catalogSearchForm .Form :LoadLayout (" layout.xml" );
145+
146+ transactionNumber = GetFieldValue (" Transaction" , " TransactionNumber" );
142147
143- local transactionNumber = GetFieldValue (" Transaction" , " TransactionNumber" );
144148 -- set the pagehandler if the user manually searches on the Browser interface directly
145149 InitializeLoginPageHandler ();
146150
147- if ((settings .AutoSearch ) and (transactionNumber ~= nil ) and (transactionNumber > 0 )) then
148- LogDebug (" Performing AutoSearch" );
149- local autoSearchPriority = ParseCSVLine (settings .AutoSearchPriority , ' ,' );
150- for _ , v in ipairs (autoSearchPriority ) do
151- -- Keep performing searches until successful
152- if (PerformSuccessfulSearch (v )) then
153- return ;
154- end
155- end
156- else
157- LogDebug (" Navigating to BaseURL because AutoSearch is disabled or invalid." );
158- catalogSearchForm .Browser :Navigate (settings .BaseURL );
159- end
160-
151+ -- We need to navigate to the main page first to detect if the user is already signed in
152+ -- AutoSearch will occur after the initial sign in attempt
153+ LogDebug (" Navigating to BaseURL first" );
154+ catalogSearchForm .Browser :Navigate (settings .BaseURL );
155+
161156end
162157
163158function ShowDevTools ()
166161
167162function InitializeLoginPageHandler ()
168163 LogDebug (" Initializing Login Page Handler" );
164+ catalogSearchForm .Browser :RegisterPageHandler (" custom" , " IsNotSignedIn" , " NavigateToLogin" , true );
165+ if (settings .AutoSearch ) then
166+ catalogSearchForm .Browser :RegisterPageHandler (" custom" , " IsSignedIn" , " AutoSearchAfterLogin" , true );
167+ else
168+ LogDebug (" AutoSearch is disabled. Skipping page page handler registration to perform autosearch functionality." )
169+ end
169170 catalogSearchForm .Browser :RegisterPageHandler (" custom" , " LoginPageLoaded" , " PerformLogin" , true );
170171 catalogSearchForm .Browser :RegisterPageHandler (" custom" , " AlwaysTrue" , " InjectScriptBridge" , false );
171172end
@@ -776,6 +777,52 @@ function WebClientGet(webClient, apiPath)
776777 return webClient :DownloadData (PathCombine (settings .ApiBaseURL , apiPath ));
777778end
778779
780+ function IsSignedIn ()
781+ return CheckIfUserSignedIn ();
782+ end
783+
784+ function IsNotSignedIn ()
785+ return not CheckIfUserSignedIn ();
786+ end
787+
788+ function CheckIfUserSignedIn ()
789+ LogDebug (" Checking if user is signed in" );
790+
791+ local jsResult = catalogSearchForm .Browser :EvaluateScript (10000 , [[ document.getElementsByClassName('user-container').length > 0]] );
792+
793+ if (jsResult .Success ) then
794+ LogDebug (" IsUserSignedIn() result: " .. tostring (jsResult .Result ));
795+ return jsResult .Result ;
796+ else
797+ LogDebug (" Error determining if user is signed in: " .. jsResult .Message );
798+ return false ;
799+ end
800+ end
801+
802+ function NavigateToLogin ()
803+ LogDebug (" Navigating to login page" );
804+ local loginUrl = PathCombine (settings .BaseURL ," ?login" )
805+ catalogSearchForm .Browser :Navigate (loginUrl );
806+ end
807+
808+ function AutoSearchAfterLogin ()
809+ LogDebug (" Checking if we need to autosearch" );
810+
811+ if ((settings .AutoSearch ) and (not performedAutoSearch ) and (transactionNumber ~= nil ) and (transactionNumber > 0 )) then
812+ LogDebug (" Performing AutoSearch" );
813+ local autoSearchPriority = ParseCSVLine (settings .AutoSearchPriority , ' ,' );
814+ for _ , v in ipairs (autoSearchPriority ) do
815+ -- Keep performing searches until successful
816+ if (PerformSuccessfulSearch (v )) then
817+ performedAutoSearch = true ;
818+ return ;
819+ end
820+ end
821+ else
822+ LogDebug (" AutoSearch is disabled or already perfored" );
823+ end
824+ end
825+
779826function LoginPageLoaded ()
780827 LogDebug (" Checking if Login Page is loaded" );
781828
793840function PerformLogin ()
794841 -- Reregister login page handler
795842 catalogSearchForm .Browser :RegisterPageHandler (" custom" , " LoginPageLoaded" , " PerformLogin" , true );
796-
843+
797844 LogDebug (" Attempting to log in." );
798845
799846 -- Anonymous function invoked with params
0 commit comments