OSDN Git Service

Redirect OSDN mail archive searches from DuckDuckGo to Google.
authorKeith Marshall <keith@users.osdn.me>
Sun, 12 Sep 2021 14:38:38 +0000 (15:38 +0100)
committerKeith Marshall <keith@users.osdn.me>
Sun, 12 Sep 2021 14:38:38 +0000 (15:38 +0100)
* site.js (osdn_archive, ddg_query, ddg_widget): New functions; they
facilitate setup and initiation of a DuckDuckGo query of any OSDN mail
archive, with redirection via the Google search engine, using...
(ddg_bang, ddg_google_search, ddg_google_search_site): ...these new
internal-use helper functions.

* mailing.html (MinGW-Users, MinGW-Notify): Use...
(ddg_widget, osdn_archive): ...these to initiate DuckDuckGo search...
(ddg_google_search): ...with this to redirect to the Google engine.

mailing.html
site.js

index 0b4d68e..2374960 100644 (file)
@@ -120,12 +120,20 @@ keyword searches within the OSDN.net (upper),
 and the SourceForge.net (lower box) archives, respectively.
 (If your question relates to MSYS, you may also wish to refer to the
 <a href="#mingw-msys">historical MinGW&#8209;MSYS archive, below</a>).
+<!-- Search widget using DuckDuckGo, does not work well on OSDN! --><!--
 <form class="search" method="get" action="https://duckduckgo.com/" target="_blank">
   <button class="fa fa-search" type="submit"></button>
   <input type="text" name="q" placeholder="Search MinGW-Users on OSDN.net ..." />
   <input type="hidden" name="sites" value="https://osdn.net/projects/mingw/lists/archive/users" />
   <input type="hidden" name="kn" value="1" />
 </form>
+--><!-- Alternative search widget, using Google, does work -->
+<form class="search" onsubmit="ddg_widget(this).click()">
+  <button class="fa fa-search" type="submit"
+    onclick="return ddg_google_search(this, true, osdn_archive('mingw', 'users'))"
+  ></button>
+  <input type="text" name="q" placeholder="Search MinGW-Users on OSDN.net ..." />
+</form>
 <form class="search" method="get" action="https://sourceforge.net/p/mingw/mailman/search/" target="_blank">
   <button class="fa fa-search" type="submit"></button>
   <input type="text" name="q" placeholder="Search MinGW-Users on SourceForge.net ..." />
@@ -155,12 +163,20 @@ while notifications posted prior to September&nbsp;2017 have been
 >archived, on SourceForge.net</a>.
 Once again, you may use the following pair of search boxes
 to perform keyword searches on these archives, respectively.
+<!-- Search widget using DuckDuckGo, does not work well on OSDN! --><!--
 <form class="search" method="get" action="https://duckduckgo.com/" target="_blank">
   <button class="fa fa-search" type="submit"></button>
   <input type="text" name="q" placeholder="Search MinGW-Notify on OSDN.net ..." />
   <input type="hidden" name="sites" value="https://osdn.net/projects/mingw/lists/archive/notify" />
   <input type="hidden" name="kn" value="1" />
 </form>
+--><!-- Alternative search widget, using Google, does work -->
+<form class="search" onsubmit="ddg_widget(this).click()">
+  <button class="fa fa-search" type="submit"
+    onclick="return ddg_google_search(this, true, osdn_archive('mingw', 'notify'))"
+  ></button>
+  <input type="text" name="q" placeholder="Search MinGW-Notify on OSDN.net ..." />
+</form>
 <form class="search" method="get" action="https://sourceforge.net/p/mingw/mailman/search/" target="_blank">
   <button class="fa fa-search" type="submit"></button>
   <input type="text" name="q" placeholder="Search MinGW-Notify on SourceForge.net ..." />
diff --git a/site.js b/site.js
index 17599e4..58399d3 100644 (file)
--- a/site.js
+++ b/site.js
@@ -98,4 +98,78 @@ function load_page( src )
   load_content( div, src );
 }
 
+/* Web-Search Widget Helper Functions
+ * ----------------------------------
+ * Derived from the DuckDuckGo.com search widget implementation by
+ * Juri Wornowitski, as described at https://www.plainlight.com/ddg,
+ * these facilitate incorporation of multiple search widget variants,
+ * at multiple locations across the site.
+ *
+ */
+function ddg_widget( ref )
+{ /* Acquire a reference to the "submit" button, within the search
+   * widget; the containing "form" element uses this to simulate a
+   * click event, if the form is submitted by pressing the "enter"
+   * key, when the "input" field has the focus.
+   */
+  ref = ref.getElementsByTagName( "button" );
+  for( idx = 0; idx < ref.length; idx++ )
+    if( ref[idx].type == "submit" ) return ref[idx];
+  return undefined;
+}
+
+function ddg_google_search_site( domain )
+{ /* Encode a search "domain" as a "site:" reference, to be appended
+   * to the query URL, when a DuckDuckGo.com search is directed to the
+   * Google search engine, via DuckDuckGo.com's "bang" facility.
+   */
+  return "+".concat( encodeURIComponent( "site:".concat( domain )));
+}
+
+function ddg_bang( src, query, domain )
+{ /* Set up a query, using DuckDuckGo.com's "bang" facility, (directed
+   * to the "bang" identified by "src"); append the query, appropriately
+   * encoded, and optionally a "domain" URL whence the query results are
+   * to be retrieved.
+   */
+  const bang = "https://duckduckgo.com?q=".concat( encodeURIComponent( "!" ));
+  query = bang.concat( src.concat( "+".concat( encodeURIComponent( query ))));
+  return query.concat( domain );
+}
+
+function ddg_query( widget )
+{ /* Retrieve the current value entered into the query "input" field;
+   * (this is identified by having a "name" property value of "q").
+   */
+  widget = widget.getElementsByTagName( "input" );
+  for( idx = 0; idx < widget.length; idx++ )
+    if( widget[idx].name == "q" ) return widget[idx].value;
+  return undefined;
+}
+
+function ddg_google_search( widget, target, domain )
+{ /* Initiate a Google search, from the DuckDuckGo.com search widget;
+   * (the "widget" reference is initially associated with the "submit"
+   * button, whence we retrieve the query context via the parent form).
+   * If "domain" is not null, it is assumed to represent a "site:" URL,
+   * where the search is to be performed, while boolean value "target"
+   * indicates whether or not a new browser window is to be opened to
+   * display the search results.
+   */
+  var query = ddg_query( widget.parentElement );
+  if( domain.length > 0 ) domain = ddg_google_search_site( domain );
+  if( target ) window.open( ddg_bang( "g", query, domain ));
+  else ddg_bang( "g", query, domain );
+  return false;
+}
+
+function osdn_archive( project, list )
+{ /* Construct a URL to represent the search domain for an OSDN.net
+   * mailing list archive, for any specified "list", belonging to a
+   * specified "project".
+   */
+  const template = "https://osdn.net/projects/%P%/lists/archive/";
+  return template.replace( "%P%", project ).concat( list );
+}
+
 /* $RCSfile$: end of file */