4 * General purpose functions for manipulation of page content.
9 * Written by Keith Marshall <keith@users.osdn.me>
10 * Copyright (C) 2020, 2021, MinGW.OSDN Project
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
20 * The above copyright notice, this permission notice, and the following
21 * disclaimer shall be included in all copies or substantial portions of
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
30 * DEALINGS IN THE SOFTWARE.
33 function set_content( item, value )
34 { /* Replace the existing content, if any, of the HTML element with
35 * id attribute named "item", (if such an element exists), with new
36 * content as specified by "value".
38 var element = document.getElementById( item );
39 if( element ) element.innerHTML = value;
43 function set_page( title, text )
44 { /* Helper function, for use in overlay page scripts, to update
45 * the "title" and "subtitle" fields within the page header; note
46 * that "text" may, and should, use ASCII hyphen-minus where any
47 * hyphen is to be represented; these will be replaced by HTML
48 * non-breaking hyphen entities, on header field assignment.
50 if( title == "title" ) document.title = text;
51 set_content( "page-".concat( title ), text.replace( /-/g, "‑" ));
54 function load_content( container, src, fallback, wanted )
55 { /* Set the content of the specified HTML "container" element, by
56 * injection of the entire contents of an external file which is
57 * fetched by http server request; (either an http, or an https,
58 * server connection is required; does not work for local files).
60 var request_handler = new XMLHttpRequest();
61 function send_e404_notification( name )
62 { var notification = set_content( "e404-missing-page", name );
63 if( notification ) notification.removeAttribute( "id" );
65 function e404_subst( name ){ return name ? name : "missing.html"; }
66 request_handler.onreadystatechange = function()
67 { if( this.readyState == this.DONE )
70 var element = set_content( container, this.responseText );
72 { var idx = 0; element = element.getElementsByTagName( "script" );
74 { var onload_action = Function( element[idx++].innerHTML );
78 else if( container ) container.innerHTML = this.responseText;
79 send_e404_notification( fallback ? wanted : document.URL );
80 if( src.includes("#") )
81 { src = src.substring( src.indexOf("#") + 1, src.length );
82 element = document.getElementById( src );
83 if( element ) element.scrollIntoView();
87 load_content( container, e404_subst( fallback ), fallback, src );
90 request_handler.open( "GET", src, true );
91 request_handler.send();
94 function load_page( src )
95 { /* Load page content from the HTML fragment file, as determined
96 * from the specified "src" URL; if no alternative fragment name
97 * is specified, fall back to loading "about.html".
100 const div = "page-content";
101 set_content( div, null );
102 load_content( "header", "header.html" );
103 if( src.includes( ref ) )
104 src = src.substring( src.indexOf( ref ) + ref.length, src.length );
105 else src = "about.html";
106 load_content( div, src );
109 /* Web-Search Widget Helper Functions
110 * ----------------------------------
111 * Derived from the DuckDuckGo.com search widget implementation by
112 * Juri Wornowitski, as described at https://www.plainlight.com/ddg,
113 * these facilitate incorporation of multiple search widget variants,
114 * at multiple locations across the site.
117 function ddg_widget( ref )
118 { /* Acquire a reference to the "submit" button, within the search
119 * widget; the containing "form" element uses this to simulate a
120 * click event, if the form is submitted by pressing the "enter"
121 * key, when the "input" field has the focus.
123 ref = ref.getElementsByTagName( "button" );
124 for( idx = 0; idx < ref.length; idx++ )
125 if( ref[idx].type == "submit" ) return ref[idx];
129 function ddg_google_search_site( domain )
130 { /* Encode a search "domain" as a "site:" reference, to be appended
131 * to the query URL, when a DuckDuckGo.com search is directed to the
132 * Google search engine, via DuckDuckGo.com's "bang" facility.
134 return "+".concat( encodeURIComponent( "site:".concat( domain )));
137 function ddg_bang( src, query, domain )
138 { /* Set up a query, using DuckDuckGo.com's "bang" facility, (directed
139 * to the "bang" identified by "src"); append the query, appropriately
140 * encoded, and optionally a "domain" URL whence the query results are
143 const bang = "https://duckduckgo.com?q=".concat( encodeURIComponent( "!" ));
144 query = bang.concat( src.concat( "+".concat( encodeURIComponent( query ))));
145 return query.concat( domain );
148 function ddg_query( widget )
149 { /* Retrieve the current value entered into the query "input" field;
150 * (this is identified by having a "name" property value of "q").
152 widget = widget.getElementsByTagName( "input" );
153 for( idx = 0; idx < widget.length; idx++ )
154 if( widget[idx].name == "q" ) return widget[idx].value;
158 function ddg_google_search( widget, target, domain )
159 { /* Initiate a Google search, from the DuckDuckGo.com search widget;
160 * (the "widget" reference is initially associated with the "submit"
161 * button, whence we retrieve the query context via the parent form).
162 * If "domain" is not null, it is assumed to represent a "site:" URL,
163 * where the search is to be performed, while boolean value "target"
164 * indicates whether or not a new browser window is to be opened to
165 * display the search results.
167 var query = ddg_query( widget.parentElement );
168 if( domain.length > 0 ) domain = ddg_google_search_site( domain );
169 if( target ) window.open( ddg_bang( "g", query, domain ));
170 else ddg_bang( "g", query, domain );
174 function osdn_archive( project, list )
175 { /* Construct a URL to represent the search domain for an OSDN.net
176 * mailing list archive, for any specified "list", belonging to a
177 * specified "project".
179 const template = "https://osdn.net/projects/%P%/lists/archive/";
180 return template.replace( "%P%", project ).concat( list );
183 /* $RCSfile$: end of file */