Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Commit

Permalink
Add robust func returning all 3 possible search results
Browse files Browse the repository at this point in the history
  • Loading branch information
spikespaz committed Jul 6, 2020
1 parent 2821f4e commit 8efa1c3
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions source/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,52 @@ string[string] parseConfig(const string config) {
return data;
}

/// Parse the query parameters from a URI and return as an associative array.
string[string] getQueryParams(const string uri) {
string[string] queryParams;

const size_t queryStart = uri.indexOf('?');

if (queryStart == -1)
return null;

const string[] paramStrings = uri[queryStart + 1 .. $].split('&');

foreach (param; paramStrings) {
const size_t equalsIndex = param.indexOf('=');
const string key = param[0 .. equalsIndex];
const string value = param[equalsIndex + 1 .. $];

queryParams[key] = value;
}

return queryParams;
}

/// Return a tuple of the search term that was typed in (if any),
/// the URL that was typed (if any), and the URL that was selected from the search results panel (if any)
Tuple!("searchTerm", string, "enteredUrl", string, "selectedUrl", string) getSearchInfo(const string uri) {
if (!uri.toLower().startsWith("microsoft-edge:"))
throw new Exception("Not a 'MICROSOFT-EDGE' URI: " ~ uri);

string[string] queryParams = getQueryParams(uri);

if (queryParams is null || "url" !in queryParams)
return null;

queryParams = getQueryParams(queryParams["url"].decodeComponent());

if ("url" in queryParams && "q" in queryParams) ///
return tuple!("searchTerm", "enteredUrl", "selectedUrl")(
queryParams["q"].decodeComponent(), null, cast(string) Base64URL.decode(queryParams["url"]));
else if ("url" in queryParams)
return tuple!("searchTerm", "enteredUrl", "selectedUrl")(null, queryParams["url"].decodeComponent(), null);
else if ("q" in queryParams)
return tuple!("searchTerm", "enteredUrl", "selectedUrl")(queryParams["q"].decodeComponent(), null, null);
else
return null;
}

/// Open a URL by spawning a shell process to the browser executable, or system default.
void openUri(const string browserPath, const string args, const string url) {
string execPath;
Expand Down

0 comments on commit 8efa1c3

Please sign in to comment.