Greasy Fork

Shined up real nice.

Google Cache Highlight Search Query Terms for HTTPS

Restore highlighted search terms in Google cache for secure searches. For Firefox+Greasemonkey or Chrome+Tampermonkey. v0.6.5 2014-05-20

After trying this script, you can ask a question about it, review it, or report it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// ==UserScript==
// @name        Google Cache Highlight Search Query Terms for HTTPS
// @author      Jefferson "jscher2000" Scher
// @namespace   JeffersonScher
// @copyright   Copyright 2014 Jefferson Scher
// @license     BSD with restriction
// @description Restore highlighted search terms in Google cache for secure searches. For Firefox+Greasemonkey or Chrome+Tampermonkey. v0.6.5 2014-05-20
// @include     https://www.google.tld/webhp*
// @include     https://www.google.tld/search*
// @version     0.6.5
// @grant       GM_log
// @grant       GM_getValue
// @grant       GM_setValue
// @grant       GM_registerMenuCommand
// @resource    mycon http://www.jeffersonscher.com/gm/src/gfrk-GCHSQT-ver065.png
// ==/UserScript==
var script_about = "https://greasyfork.org/scripts/1681-google-cache-highlight-search-query-terms-for-https";
/*
Copyright (c) 2014 Jefferson Scher. All rights reserved.

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:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

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.

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.

RESTRICTION: USE WITH ANY @include or @match THAT COVERS FACEBOOK.COM IS PROHIBITED AND UNLICENSED.

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.
*/

// Make "Cached" link persistently visible -- not essential to functionality
var mysty = document.createElement("style");
mysty.appendChild(document.createTextNode(".vshid{display:inline;margin-left:6px;}")); // old layout
document.body.appendChild(mysty);

// Get/set preferences
var defaultPrefs = {
  "ssl":["Y","Use HTTPS for cache page for privacy — linked files may fail to load","(Y|N)"],
  "phrases":["Y","Keep quoted phrases together — do not highlight the words appearing alone","(Y|N)"],
  "pref3":["Y","TBD","TBD"],
  "actionstyle":["N","Convert action menu to show Cached and Similar links inline","(Y|N)"],
  "pref5":["0","TBD","TBD"]
};
var prefs = GM_getValue("prefs");
if (!prefs || prefs.length == 0){
  prefs = JSON.stringify(defaultPrefs);
  GM_setValue("prefs", prefs);
}
var oPrefs = JSON.parse(prefs);

// Convert drop-down action menu to inline v0.6.4
if (oPrefs.pref4) oPrefs = GCHSQTfH_convertPrefs(defaultPrefs, oPrefs);
if (oPrefs.actionstyle[0] == "Y"){
  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;}"));
}

// Add menu item
GM_registerMenuCommand("Preferences for Google Cache Highlight", openPrefs);

// Add MutationObserver to catch AJAX results
var GCHSQTfH_MutOb = (window.MutationObserver) ? window.MutationObserver : window.WebKitMutationObserver;
if (GCHSQTfH_MutOb){
  var GCHSQTfH_chgMon = new GCHSQTfH_MutOb(function(mutationSet){
    mutationSet.forEach(function(mutation){
      for (var i=0; i<mutation.addedNodes.length; i++){
        if (mutation.addedNodes[i].nodeType == 1){
          GCHSQTfH_checkNode(mutation.addedNodes[i]);
        }
      }
    });
  });
  // attach chgMon to document.body
  var opts = {childList: true, subtree: true};
  GCHSQTfH_chgMon.observe(document.body, opts);
}
// Check added element for probable Cached links
function GCHSQTfH_checkNode(el){
  var clinks = el.querySelectorAll("span.vshid a:first-child, li.action-menu-item a.fl, span.flc a");
  if (clinks.length > 0) GCHSQTfH_fixlinks(clinks);
}
// Edit the Cached links
function GCHSQTfH_fixlinks(spanarray){
  // Add a mouseover listener to fix the href
  for (var j=0; j<spanarray.length; j++){
    spanarray[j].addEventListener("mouseover", GCHSQTfH_addQryTerms, false);
  }
}
function GCHSQTfH_addQryTerms(e){
  if (e.target.nodeName != "A") return;
  var qry = GCHSQTfH_getQuery();
  var savedQry = e.target.getAttribute("GCHSQTfH");
  if (!savedQry) savedQry = "";
  if (savedQry == qry) return;
  // v0.6.2 Remove incorrectly embedded query terms in heirloom serp mode 
  var qsp = window.location.href.indexOf("?");
  var q0 = "";
  if (qsp > 0){
    var qs = window.location.href.substr(qsp+1);
    var qa = qs.split("&");
    for (var j=0; j<qa.length; j++){
      if (qa[j].split("=")[0] == "q"){
        q0 = qa[j].split("=")[1];
      }
      if (qa[j].indexOf("#q=") > -1){
        q0 = qa[j].split("=")[1];
      }
    }
    if (q0 != ""){
      var qryEnc = "%2B" + q0 + "&";
      e.target.setAttribute("href", e.target.getAttribute("href").replace(qryEnc, "+&"));
    }
  }
  // Update href to use HTTPS cache (per user preference) and include query terms in the link
  if (savedQry != ""){
    e.target.setAttribute("href", e.target.getAttribute("href").replace(savedQry+"&", qry+"&"));
  } else {
    if (oPrefs.ssl[0] == "Y"){
      e.target.setAttribute("href", e.target.getAttribute("href").replace("http://webcache.", "https://webcache."));
    }
    e.target.setAttribute("href", e.target.getAttribute("href").replace("+&", "+"+qry+"&"));
  }
  e.target.setAttribute("GCHSQTfH", qry);
  if (e.target.hasAttribute("onmousedown")) e.target.removeAttribute("onmousedown"); // v0.5.4
}
// Try to get the best set of query terms for matching
function GCHSQTfH_getQuery(){
  // Find current query terms
  // Check for corrected spelling first, then query form
  var spell_mod = document.querySelector("span#taw a.spell");
  if (spell_mod && spell_mod.parentNode.querySelector("span.spell_orig")){
    return GCHSQTfH_cleanQuery(spell_mod.textContent.replace(/\+/g, "%2B").replace(/\s/g, "+"));
  } else {
    var qbox = document.querySelector("form[action='/search'] input[name='q']");
    if (qbox){
      var q = qbox.value.replace(/\+/g, "%2B").replace(/\s/g, "+");
      if (q.length > 0) return GCHSQTfH_cleanQuery(q);
    }
    var qsp = window.location.href.indexOf("?");
    if (qsp > 0){
      var qs = window.location.href.substr(qsp+1);
      var qa = qs.split("&");
      for (var j=0; j<qa.length; j++){
        if (qa[j].split("=")[0] == "q"){
          var q0 = qa[j].split("=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
        }
        if (qa[j].indexOf("#q=") > -1){
          var q0 = qa[j].split("#q=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
        }
      }
      if (q0) return GCHSQTfH_cleanQuery(q0);
      else return "";
    }
  }
}
function GCHSQTfH_cleanQuery(s){
  var pos1 = s.indexOf('-"');
  var pos2, tmp = "", t2 = "", ret;
  while (pos1 > -1){
    pos2 = s.indexOf('"', pos1+2);
    if (pos2 == -1) break;
    if (pos2 > pos1){
      if (pos1 > 0) tmp = s.substr(0, pos1);
      if (pos2 < s.length-2){
        if (tmp.length>0) tmp += s.substr(pos2+1);
        else tmp += s.substr(pos2+2);
      }
      s = tmp;
    }
    pos1 = s.indexOf('-"');
  }
  var pos1 = s.indexOf('"');
  while (pos1 > -1){
    pos2 = s.indexOf('"', pos1+1);
    if (pos2 == -1) break;
    if (pos2 > pos1){
      if (pos1 > 0) tmp = s.substr(0, pos1);
      t2 = s.substr(pos1+1, pos2-pos1-1);
      if (oPrefs.phrases[0] == "Y"){
        t2 = t2.replace(/\+/g, "-"); //sticks phrase together
      }
      if (pos2 < s.length-2){
        if (tmp.length>0) tmp += t2 + "+" + s.substr(pos2+1);
        else tmp += t2 + "+" + s.substr(pos2+2);
      } else {
        tmp += t2;
      }
      s = tmp;
    }
    pos1 = s.indexOf('"');
  }
  s = s.replace(/"/g, "");
  var terms = s.replace(/\++/g, "+").split("+");
  for (var k=0; k<terms.length; k++){
    if (terms[k].indexOf("-")==0 || terms[k].indexOf("site:")==0 || 
        terms[k].indexOf("inurl:")==0 || 
        terms[k].toLowerCase()=="or" || terms[k]=="&") terms[k] = "";
  }
  while (terms[terms.length-1]=="") ret = terms.pop();
  s = terms.join("+");
  return s.replace(/\++/g, "+").replace("&", "%26").replace("#", "%23"); // 0.6.3 fix #
}
// Initial run to add event listeners
GCHSQTfH_checkNode(document.body);

function openPrefs(e){
  var pform = document.getElementById("GCHSQTfH_pform");
  if (pform){
    pform.display = "block";
  } else {
    pform = document.createElement("div");
    pform.id = "GCHSQTfH_pform";
    pform.setAttribute("style", "position:fixed;bottom:0;background:#00f;color:#fff;font-weight:bold;font-size:20px;width:100%;z-index:999");
    pform.innerHTML= "<form onsubmit=\"return false;\"><button style=\"float:right\" id=\"btncacheClose\">X</button>" +
    "<label title=\"Switch between HTTP and HTTPS cache views\"><input " +
    "type=\"checkbox\" name=\"chkcacheSSL\" id=\"chkcacheSSL\"> " + oPrefs.ssl[1] + "</label><br>" +
    "<label title=\"Switch between phrases and separate words\"><input type=\"checkbox\" name=\"chkcachePhrases\" " +
    "id=\"chkcachePhrases\"> " + oPrefs.phrases[1] + "</label><br>" +
    "<label title=\"Convert action menu to inline display\"><input type=\"checkbox\" name=\"chkcacheActionStyle\" " +
    "id=\"chkcacheActionStyle\"> " + oPrefs.actionstyle[1] + " (reload after changing)</label></form>";
    document.body.appendChild(pform);
  }
  GCHSQTfH_fixssl();
  GCHSQTfH_fixphrases();
  GCHSQTfH_fixactionstyle();
  document.getElementById("btncacheClose").addEventListener("click", GCHSQTfH_closeprefs, false);
  document.getElementById("chkcacheSSL").addEventListener("change", GCHSQTfH_updtssl, false);
  document.getElementById("chkcachePhrases").addEventListener("change", GCHSQTfH_updtphrases, false);
  document.getElementById("chkcacheActionStyle").addEventListener("change", GCHSQTfH_updtactionstyle, false);
}
function GCHSQTfH_updtssl(e){ // Store setting for HTTP vs. HTTPS cached pages
  var chk = e.target;
  if (chk.checked){
    oPrefs.ssl[0] = "Y";
  } else {
    oPrefs.ssl[0] = "N";
  }
  GM_setValue("prefs", JSON.stringify(oPrefs));
  GCHSQTfH_fixssl();
}
function GCHSQTfH_fixssl(){  // Adjust checkbox for HTTP vs. HTTPS cached pages
  var chk = document.getElementById("chkcacheSSL");
  if (oPrefs.ssl[0] == "Y"){
    chk.setAttribute("checked","checked");
    chk.checked = true;
  } else {
    chk.removeAttribute("checked");
    chk.checked = false;  
  }
}
function GCHSQTfH_updtphrases(e){ // Store setting for phrases vs individual words
  var chk = e.target;
  if (chk.checked){
    oPrefs.phrases[0] = "Y";
  } else {
    oPrefs.phrases[0] = "N";
  }
  GM_setValue("prefs", JSON.stringify(oPrefs));
  GCHSQTfH_fixphrases();
}
function GCHSQTfH_fixphrases(){  // Adjust checkbox for phrases vs individual words
  var chk = document.getElementById("chkcachePhrases");
  if (oPrefs.phrases[0] == "Y"){
    chk.setAttribute("checked","checked");
    chk.checked = true;
  } else {
    chk.removeAttribute("checked");
    chk.checked = false;  
  }
}
function GCHSQTfH_updtactionstyle(e){ // Store setting for drop-down vs inline action links v0.6.4
  var chk = e.target;
  if (chk.checked){
    oPrefs.actionstyle[0] = "Y";
  } else {
    oPrefs.actionstyle[0] = "N";
  }
  GM_setValue("prefs", JSON.stringify(oPrefs));
  GCHSQTfH_fixactionstyle();
}
function GCHSQTfH_fixactionstyle(){  // Adjust checkbox for drop-down vs inline action links v0.6.4
  var chk = document.getElementById("chkcacheActionStyle");
  if (oPrefs.actionstyle[0] == "Y"){
    chk.setAttribute("checked","checked");
    chk.checked = true;
  } else {
    chk.removeAttribute("checked");
    chk.checked = false;  
  }
}
function GCHSQTfH_closeprefs(e){
  e.target.parentNode.parentNode.style.display = "none";
}
function GCHSQTfH_convertPrefs(oDefault, oUser){ // Conform old objects to new defaults preserving values v0.6.4
  oDefault.ssl[0] = oUser.ssl[0];
  oDefault.phrases[0] = oUser.phrases[0];
  if (oUser.pref4){
    oDefault.actionstyle[0] = oUser.pref4[0];
  } else {
    oDefault.actionstyle[0] = oUser.actionstyle[0];
  }
  GM_setValue("prefs",JSON.stringify(oDefault));
  return oDefault;
}