Skip to content

Commit

Permalink
Fix search navigation to use Query URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalsada committed Nov 18, 2021
1 parent 1210af1 commit bdbede2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 40 deletions.
2 changes: 1 addition & 1 deletion Config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Internet Archive Search</Name>
<Author>Atlas Systems and Simmons College Library</Author>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<Active>true</Active>
<Description>Performs a search of internet archive for a loans' LoanTitle.</Description>
<Forms>
Expand Down
56 changes: 17 additions & 39 deletions InternetArchiveSearch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,54 +52,32 @@ function Init()
addonForm.Form:Show();

if autoSearch then
LogDebug("AutoSearch is enabled.");
Search();
else
LogDebug("AutoSearch is disabled. Showing Internet Archive base page");
addonForm.Browser:Navigate("https://archive.org");
end
else
LogDebug("Internet Archive Search addon not displayed. Addon is only show for Loan requests.");
end
end

function Search()
addonForm.Browser:RegisterPageHandler("formExists", "searchform","SearchFormLoaded", false);
addonForm.Browser:Navigate("http://archive.org");
local searchUrl = "https://archive.org/search.php?query=" .. UrlEncode(GetFieldValue("Transaction", "LoanTitle"));
addonForm.Browser:Navigate(searchUrl);
end

function SearchFormLoaded()
if GetFieldValue("Transaction", "RequestType") == "Loan" then
SearchInternetArchives(addonForm.Browser, "searchform", "search", GetFieldValue("Transaction", "LoanTitle"));
end
end

function SearchInternetArchives(browser, formName, inputName, value)
if browser then
--Script to execute
local searchInternetArchives = [[
(function(formName, inputName, value) {
//Write to the client log.
//Use ES6 Template Strings to concatenate - using back-ticks: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings
atlasAddonAsync.executeAddonFunction("LogDebug", `Performing Internet Archives Search: ${value}` );
let form = document.forms[formName];
if (!(form)) {
atlasAddonAsync.executeAddonFunction("LogDebug", `Unable to find form: ${formName}` );
return;
}
let inputElement = form.elements[inputName];
if (!(inputElement)) {
atlasAddonAsync.executeAddonFunction("LogDebug", `Unable to find form input: ${inputName}` );
return;
}
inputElement.value = value;
//Internet Archive masks form submit with a hidden element with name "submit"
//Instead, look for a the button and click it
if (form.getElementsByTagName("button").length == 1) {
form.getElementsByTagName("button")[0].click();
}
})
]];

browser:ExecuteScript(searchInternetArchives, { formName, inputName, value });
function UrlEncode(s)
if (s) then
s = string.gsub(s, "\n", "\r\n")
s = string.gsub(s, "([^%w %-%_%.%~])",
function (c)
return string.format("%%%02X", string.byte(c))
end);
s = string.gsub(s, " ", "+")
end
return s
end

function OpenInDefaultBrowser()
Expand Down

0 comments on commit bdbede2

Please sign in to comment.