Google Cache Highlight Search Query Terms for HTTPS

Restore highlighted search terms in Google cache for secure searches. For Firefox+Violentmonkey or Chrome+Tampermonkey. v0.6.6 2023-06-19

  1. // ==UserScript==
  2. // @name Google Cache Highlight Search Query Terms for HTTPS
  3. // @author Jefferson "jscher2000" Scher
  4. // @namespace JeffersonScher
  5. // @copyright Copyright 2023 Jefferson Scher
  6. // @license BSD with restriction
  7. // @description Restore highlighted search terms in Google cache for secure searches. For Firefox+Violentmonkey or Chrome+Tampermonkey. v0.6.6 2023-06-19
  8. // @include https://www.google.tld/webhp*
  9. // @include https://www.google.tld/search*
  10. // @version 0.6.6
  11. // @grant GM_log
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_registerMenuCommand
  15. // @resource mycon http://www.jeffersonscher.com/gm/src/gfrk-GCHSQT-ver066.png
  16. // ==/UserScript==
  17. var script_about = "https://greasyfork.org/scripts/1681-google-cache-highlight-search-query-terms-for-https";
  18. /*
  19. Copyright (c) 2023 Jefferson Scher. All rights reserved.
  20.  
  21. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met and subject to the following restriction:
  22.  
  23. 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  24.  
  25. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  26.  
  27. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  28.  
  29. RESTRICTION: USE WITH ANY @include or @match THAT COVERS FACEBOOK.COM IS PROHIBITED AND UNLICENSED.
  30.  
  31. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33.  
  34. // Make "Cached" link persistently visible -- not essential to functionality // DOES NOT WORK WITH NEW 3-DOT MENU
  35. var mysty = document.createElement("style");
  36. mysty.appendChild(document.createTextNode('.vshid,g-menu[role="mnenu"]{display:inline;margin-left:6px;} .cachedlink{margin-left:40px;}')); // old layout + new synthetic link
  37. document.body.appendChild(mysty);
  38.  
  39. // Get/set preferences
  40. var defaultPrefs = {
  41. "ssl":["Y","Use HTTPS for cache page for privacy — linked files may fail to load","(Y|N)"],
  42. "phrases":["Y","Keep quoted phrases together — do not highlight the words appearing alone","(Y|N)"],
  43. "pref3":["Y","TBD","TBD"],
  44. "actionstyle":["N","Convert action menu to show Cached and Similar links inline","(Y|N)"],
  45. "pref5":["0","TBD","TBD"]
  46. };
  47. var prefs = GM_getValue("prefs");
  48. if (!prefs || prefs.length == 0){
  49. prefs = JSON.stringify(defaultPrefs);
  50. GM_setValue("prefs", prefs);
  51. }
  52. var oPrefs = JSON.parse(prefs);
  53.  
  54. // Convert drop-down action menu to inline v0.6.4
  55. if (oPrefs.pref4) oPrefs = GCHSQTfH_convertPrefs(defaultPrefs, oPrefs);
  56. if (oPrefs.actionstyle[0] == "Y"){
  57. mysty.appendChild(document.createTextNode(".action-menu {vertical-align:baseline !important;} .action-menu .clickable-dropdown-arrow {display:none !important;} .action-menu-panel, .action-menu-panel ul, .action-menu-item {display:inline !important; visibility: visible !important; border:none !important; box-shadow:none !important; background-color:transparent !important; margin:0 !important; padding:0 !important; top:0 !important; height:auto !important; line-height:auto !important;} .action-menu-item a.fl, .action-menu-button {padding:0 0 0 6px !important; display:inline !important;} .action-menu-panel {position:static;} .action-menu-item a.fl:hover {text-decoration:underline !important;}"));
  58. }
  59.  
  60. // Add menu item
  61. GM_registerMenuCommand("Preferences for Google Cache Highlight", openPrefs);
  62.  
  63. // Add MutationObserver to catch AJAX results
  64. var GCHSQTfH_MutOb = (window.MutationObserver) ? window.MutationObserver : window.WebKitMutationObserver;
  65. if (GCHSQTfH_MutOb){
  66. var GCHSQTfH_chgMon = new GCHSQTfH_MutOb(function(mutationSet){
  67. mutationSet.forEach(function(mutation){
  68. for (var i=0; i<mutation.addedNodes.length; i++){
  69. if (mutation.addedNodes[i].nodeType == 1){
  70. GCHSQTfH_checkNode(mutation.addedNodes[i]);
  71. }
  72. }
  73. });
  74. });
  75. // attach chgMon to document.body
  76. var opts = {childList: true, subtree: true};
  77. GCHSQTfH_chgMon.observe(document.body, opts);
  78. }
  79. // Check added element for probable Cached links
  80. function GCHSQTfH_checkNode(el){
  81. var clinks = el.querySelectorAll('span.vshid a:first-child, li.action-menu-item a.fl, span.flc a, a[href*="https://webcache.googleusercontent.com/search?q=cache:"]');
  82. if (clinks.length > 0){
  83. GCHSQTfH_fixlinks(clinks);
  84. } else { // 3-dot stack menu replaced the classic menu some time ago, so... v0.6.6
  85. var results = el.querySelectorAll('div.g');
  86. GCHSQTfH_createlinks(results);
  87. }
  88. }
  89. // Try to inject "Cached" links v0.6.6
  90. function GCHSQTfH_createlinks(resultdivs){
  91. for (var j=0; j<resultdivs.length; j++){
  92. var svgs = resultdivs[j].querySelectorAll('svg'); // BREAKABLE assumes Google will keep using an SVG element for the 3-dot stack
  93. if (svgs.length > 0){
  94. var linkparent = svgs[0].closest('div[jscontroller]'); // BREAKABLE assumes Google will continue to have a jscontroller attribute on the parent div
  95. if (linkparent && resultdivs[j].querySelectorAll('a').length > 0){
  96. var resultlink = resultdivs[j].querySelectorAll('a')[0].href;
  97. var cachedlink = document.createElement('a');
  98. cachedlink.setAttribute('href', 'https://webcache.googleusercontent.com/search?q=cache:' + resultlink);
  99. cachedlink.setAttribute('target', '_blank');
  100. cachedlink.classList.add('cachedlink');
  101. cachedlink.textContent = 'Cached';
  102. linkparent.appendChild(cachedlink);
  103. // This is for completeness, but Google seems to have stopped highlighting terms for us
  104. cachedlink.addEventListener("mouseover", GCHSQTfH_addQryTerms, false);
  105. }
  106. }
  107. }
  108. }
  109. // Edit the Cached links
  110. function GCHSQTfH_fixlinks(spanarray){
  111. // Add a mouseover listener to fix the href
  112. for (var j=0; j<spanarray.length; j++){
  113. spanarray[j].addEventListener("mouseover", GCHSQTfH_addQryTerms, false);
  114. }
  115. }
  116. function GCHSQTfH_addQryTerms(e){
  117. var tgt = e.target;
  118. if (tgt.nodeName != 'A'){
  119. tgt = tgt.closest('a');
  120. if (tgt.nodeName != 'A') return;
  121. }
  122. var qry = GCHSQTfH_getQuery();
  123. var savedQry = e.target.getAttribute("GCHSQTfH");
  124. if (!savedQry) savedQry = "";
  125. if (savedQry == qry) return;
  126. // v0.6.2 Remove incorrectly embedded query terms in heirloom serp mode
  127. var qsp = window.location.href.indexOf("?");
  128. var q0 = "";
  129. if (qsp > 0){
  130. var qs = window.location.href.substr(qsp+1);
  131. var qa = qs.split("&");
  132. for (var j=0; j<qa.length; j++){
  133. if (qa[j].split("=")[0] == "q"){
  134. q0 = qa[j].split("=")[1];
  135. }
  136. if (qa[j].indexOf("#q=") > -1){
  137. q0 = qa[j].split("=")[1];
  138. }
  139. }
  140. if (q0 != ""){
  141. var qryEnc = "%2B" + q0 + "&";
  142. tgt.setAttribute("href", tgt.getAttribute("href").replace(qryEnc, "+&"));
  143. }
  144. }
  145. // Update href to use HTTPS cache (per user preference) and include query terms in the link
  146. console.log('savedQry=' + savedQry);
  147. if (savedQry != ""){
  148. tgt.setAttribute("href", tgt.getAttribute("href").replace(savedQry+"&", qry+"&"));
  149. } else {
  150. if (oPrefs.ssl[0] == "Y"){
  151. tgt.setAttribute("href", tgt.getAttribute("href").replace("http://webcache.", "https://webcache."));
  152. }
  153. if (tgt.getAttribute("href").indexOf('+&') > -1) tgt.setAttribute("href", tgt.getAttribute("href").replace("+&", "+"+qry+"&"));
  154. else {
  155. var hrefparts = tgt.getAttribute("href").split('&');
  156. hrefparts[0] += '+' + qry;
  157. tgt.setAttribute("href", hrefparts.join('&'));
  158. }
  159. }
  160. tgt.setAttribute("GCHSQTfH", qry);
  161. if (tgt.hasAttribute("onmousedown")) tgt.removeAttribute("onmousedown"); // v0.5.4
  162. }
  163. // Try to get the best set of query terms for matching
  164. function GCHSQTfH_getQuery(){
  165. // Find current query terms
  166. // Check for corrected spelling first, then query form
  167. var spell_mod = document.querySelector("span#taw a.spell");
  168. if (spell_mod && spell_mod.parentNode.querySelector("span.spell_orig")){
  169. return GCHSQTfH_cleanQuery(spell_mod.textContent.replace(/\+/g, "%2B").replace(/\s/g, "+"));
  170. } else {
  171. var qbox = document.querySelector("form[action='/search'] input[name='q']");
  172. if (qbox){
  173. var q = qbox.value.replace(/\+/g, "%2B").replace(/\s/g, "+");
  174. if (q.length > 0) return GCHSQTfH_cleanQuery(q);
  175. }
  176. var qsp = window.location.href.indexOf("?");
  177. if (qsp > 0){
  178. var qs = window.location.href.substr(qsp+1);
  179. var qa = qs.split("&");
  180. for (var j=0; j<qa.length; j++){
  181. if (qa[j].split("=")[0] == "q"){
  182. var q0 = qa[j].split("=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
  183. }
  184. if (qa[j].indexOf("#q=") > -1){
  185. var q0 = qa[j].split("#q=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
  186. }
  187. }
  188. if (q0) return GCHSQTfH_cleanQuery(q0);
  189. else return "";
  190. }
  191. }
  192. }
  193. function GCHSQTfH_cleanQuery(s){
  194. var pos1 = s.indexOf('-"');
  195. var pos2, tmp = "", t2 = "", ret;
  196. while (pos1 > -1){
  197. pos2 = s.indexOf('"', pos1+2);
  198. if (pos2 == -1) break;
  199. if (pos2 > pos1){
  200. if (pos1 > 0) tmp = s.substr(0, pos1);
  201. if (pos2 < s.length-2){
  202. if (tmp.length>0) tmp += s.substr(pos2+1);
  203. else tmp += s.substr(pos2+2);
  204. }
  205. s = tmp;
  206. }
  207. pos1 = s.indexOf('-"');
  208. }
  209. var pos1 = s.indexOf('"');
  210. while (pos1 > -1){
  211. pos2 = s.indexOf('"', pos1+1);
  212. if (pos2 == -1) break;
  213. if (pos2 > pos1){
  214. if (pos1 > 0) tmp = s.substr(0, pos1);
  215. t2 = s.substr(pos1+1, pos2-pos1-1);
  216. if (oPrefs.phrases[0] == "Y"){
  217. t2 = t2.replace(/\+/g, "-"); //sticks phrase together
  218. }
  219. if (pos2 < s.length-2){
  220. if (tmp.length>0) tmp += t2 + "+" + s.substr(pos2+1);
  221. else tmp += t2 + "+" + s.substr(pos2+2);
  222. } else {
  223. tmp += t2;
  224. }
  225. s = tmp;
  226. }
  227. pos1 = s.indexOf('"');
  228. }
  229. s = s.replace(/"/g, "");
  230. var terms = s.replace(/\++/g, "+").split("+");
  231. for (var k=0; k<terms.length; k++){
  232. if (terms[k].indexOf("-")==0 || terms[k].indexOf("site:")==0 ||
  233. terms[k].indexOf("inurl:")==0 ||
  234. terms[k].toLowerCase()=="or" || terms[k]=="&") terms[k] = "";
  235. }
  236. while (terms[terms.length-1]=="") ret = terms.pop();
  237. s = terms.join("+");
  238. return s.replace(/\++/g, "+").replace("&", "%26").replace("#", "%23"); // 0.6.3 fix #
  239. }
  240. // Initial run to add event listeners
  241. GCHSQTfH_checkNode(document.body);
  242.  
  243. function openPrefs(e){
  244. var pform = document.getElementById("GCHSQTfH_pform");
  245. if (pform){
  246. pform.display = "block";
  247. } else {
  248. pform = document.createElement("div");
  249. pform.id = "GCHSQTfH_pform";
  250. pform.setAttribute("style", "position:fixed;bottom:0;background:#00f;color:#fff;font-weight:bold;font-size:20px;width:100%;z-index:999");
  251. pform.innerHTML= "<form onsubmit=\"return false;\"><button style=\"float:right\" id=\"btncacheClose\">X</button>" +
  252. "<label title=\"Switch between HTTP and HTTPS cache views\"><input " +
  253. "type=\"checkbox\" name=\"chkcacheSSL\" id=\"chkcacheSSL\"> " + oPrefs.ssl[1] + "</label><br>" +
  254. "<label title=\"Switch between phrases and separate words\"><input type=\"checkbox\" name=\"chkcachePhrases\" " +
  255. "id=\"chkcachePhrases\"> " + oPrefs.phrases[1] + "</label><br>" +
  256. "<label title=\"Convert action menu to inline display\"><input type=\"checkbox\" name=\"chkcacheActionStyle\" " +
  257. "id=\"chkcacheActionStyle\"> " + oPrefs.actionstyle[1] + " (reload after changing)</label></form>";
  258. document.body.appendChild(pform);
  259. }
  260. GCHSQTfH_fixssl();
  261. GCHSQTfH_fixphrases();
  262. GCHSQTfH_fixactionstyle();
  263. document.getElementById("btncacheClose").addEventListener("click", GCHSQTfH_closeprefs, false);
  264. document.getElementById("chkcacheSSL").addEventListener("change", GCHSQTfH_updtssl, false);
  265. document.getElementById("chkcachePhrases").addEventListener("change", GCHSQTfH_updtphrases, false);
  266. document.getElementById("chkcacheActionStyle").addEventListener("change", GCHSQTfH_updtactionstyle, false);
  267. }
  268. function GCHSQTfH_updtssl(e){ // Store setting for HTTP vs. HTTPS cached pages
  269. var chk = e.target;
  270. if (chk.checked){
  271. oPrefs.ssl[0] = "Y";
  272. } else {
  273. oPrefs.ssl[0] = "N";
  274. }
  275. GM_setValue("prefs", JSON.stringify(oPrefs));
  276. GCHSQTfH_fixssl();
  277. }
  278. function GCHSQTfH_fixssl(){ // Adjust checkbox for HTTP vs. HTTPS cached pages
  279. var chk = document.getElementById("chkcacheSSL");
  280. if (oPrefs.ssl[0] == "Y"){
  281. chk.setAttribute("checked","checked");
  282. chk.checked = true;
  283. } else {
  284. chk.removeAttribute("checked");
  285. chk.checked = false;
  286. }
  287. }
  288. function GCHSQTfH_updtphrases(e){ // Store setting for phrases vs individual words
  289. var chk = e.target;
  290. if (chk.checked){
  291. oPrefs.phrases[0] = "Y";
  292. } else {
  293. oPrefs.phrases[0] = "N";
  294. }
  295. GM_setValue("prefs", JSON.stringify(oPrefs));
  296. GCHSQTfH_fixphrases();
  297. }
  298. function GCHSQTfH_fixphrases(){ // Adjust checkbox for phrases vs individual words
  299. var chk = document.getElementById("chkcachePhrases");
  300. if (oPrefs.phrases[0] == "Y"){
  301. chk.setAttribute("checked","checked");
  302. chk.checked = true;
  303. } else {
  304. chk.removeAttribute("checked");
  305. chk.checked = false;
  306. }
  307. }
  308. function GCHSQTfH_updtactionstyle(e){ // Store setting for drop-down vs inline action links v0.6.4
  309. var chk = e.target;
  310. if (chk.checked){
  311. oPrefs.actionstyle[0] = "Y";
  312. } else {
  313. oPrefs.actionstyle[0] = "N";
  314. }
  315. GM_setValue("prefs", JSON.stringify(oPrefs));
  316. GCHSQTfH_fixactionstyle();
  317. }
  318. function GCHSQTfH_fixactionstyle(){ // Adjust checkbox for drop-down vs inline action links v0.6.4
  319. var chk = document.getElementById("chkcacheActionStyle");
  320. if (oPrefs.actionstyle[0] == "Y"){
  321. chk.setAttribute("checked","checked");
  322. chk.checked = true;
  323. } else {
  324. chk.removeAttribute("checked");
  325. chk.checked = false;
  326. }
  327. }
  328. function GCHSQTfH_closeprefs(e){
  329. e.target.parentNode.parentNode.style.display = "none";
  330. }
  331. function GCHSQTfH_convertPrefs(oDefault, oUser){ // Conform old objects to new defaults preserving values v0.6.4
  332. oDefault.ssl[0] = oUser.ssl[0];
  333. oDefault.phrases[0] = oUser.phrases[0];
  334. if (oUser.pref4){
  335. oDefault.actionstyle[0] = oUser.pref4[0];
  336. } else {
  337. oDefault.actionstyle[0] = oUser.actionstyle[0];
  338. }
  339. GM_setValue("prefs",JSON.stringify(oDefault));
  340. return oDefault;
  341. }