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

目前為 2014-05-25 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Google Cache Highlight Search Query Terms for HTTPS
  3. // @author Jefferson "jscher2000" Scher
  4. // @namespace JeffersonScher
  5. // @copyright Copyright 2014 Jefferson Scher
  6. // @license BSD with restriction
  7. // @description Restore highlighted search terms in Google cache for secure searches. For Firefox+Greasemonkey or Chrome+Tampermonkey. v0.6.5 2014-05-20
  8. // @include https://www.google.tld/webhp*
  9. // @include https://www.google.tld/search*
  10. // @version 0.6.5
  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-ver065.png
  16. // ==/UserScript==
  17. var script_about = "https://gf.qytechs.cn/scripts/SCRIPTURL";
  18. /*
  19. Copyright (c) 2014 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
  35. var mysty = document.createElement("style");
  36. mysty.appendChild(document.createTextNode(".vshid{display:inline;margin-left:6px;}")); // old layout
  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");
  82. if (clinks.length > 0) GCHSQTfH_fixlinks(clinks);
  83. }
  84. // Edit the Cached links
  85. function GCHSQTfH_fixlinks(spanarray){
  86. // Add a mouseover listener to fix the href
  87. for (var j=0; j<spanarray.length; j++){
  88. spanarray[j].addEventListener("mouseover", GCHSQTfH_addQryTerms, false);
  89. }
  90. }
  91. function GCHSQTfH_addQryTerms(e){
  92. if (e.target.nodeName != "A") return;
  93. var qry = GCHSQTfH_getQuery();
  94. var savedQry = e.target.getAttribute("GCHSQTfH");
  95. if (!savedQry) savedQry = "";
  96. if (savedQry == qry) return;
  97. // v0.6.2 Remove incorrectly embedded query terms in heirloom serp mode
  98. var qsp = window.location.href.indexOf("?");
  99. var q0 = "";
  100. if (qsp > 0){
  101. var qs = window.location.href.substr(qsp+1);
  102. var qa = qs.split("&");
  103. for (var j=0; j<qa.length; j++){
  104. if (qa[j].split("=")[0] == "q"){
  105. q0 = qa[j].split("=")[1];
  106. }
  107. if (qa[j].indexOf("#q=") > -1){
  108. q0 = qa[j].split("=")[1];
  109. }
  110. }
  111. if (q0 != ""){
  112. var qryEnc = "%2B" + q0 + "&";
  113. e.target.setAttribute("href", e.target.getAttribute("href").replace(qryEnc, "+&"));
  114. }
  115. }
  116. // Update href to use HTTPS cache (per user preference) and include query terms in the link
  117. if (savedQry != ""){
  118. e.target.setAttribute("href", e.target.getAttribute("href").replace(savedQry+"&", qry+"&"));
  119. } else {
  120. if (oPrefs.ssl[0] == "Y"){
  121. e.target.setAttribute("href", e.target.getAttribute("href").replace("http://webcache.", "https://webcache."));
  122. }
  123. e.target.setAttribute("href", e.target.getAttribute("href").replace("+&", "+"+qry+"&"));
  124. }
  125. e.target.setAttribute("GCHSQTfH", qry);
  126. if (e.target.hasAttribute("onmousedown")) e.target.removeAttribute("onmousedown"); // v0.5.4
  127. }
  128. // Try to get the best set of query terms for matching
  129. function GCHSQTfH_getQuery(){
  130. // Find current query terms
  131. // Check for corrected spelling first, then query form
  132. var spell_mod = document.querySelector("span#taw a.spell");
  133. if (spell_mod && spell_mod.parentNode.querySelector("span.spell_orig")){
  134. return GCHSQTfH_cleanQuery(spell_mod.textContent.replace(/\+/g, "%2B").replace(/\s/g, "+"));
  135. } else {
  136. var qbox = document.querySelector("form[action='/search'] input[name='q']");
  137. if (qbox){
  138. var q = qbox.value.replace(/\+/g, "%2B").replace(/\s/g, "+");
  139. if (q.length > 0) return GCHSQTfH_cleanQuery(q);
  140. }
  141. var qsp = window.location.href.indexOf("?");
  142. if (qsp > 0){
  143. var qs = window.location.href.substr(qsp+1);
  144. var qa = qs.split("&");
  145. for (var j=0; j<qa.length; j++){
  146. if (qa[j].split("=")[0] == "q"){
  147. var q0 = qa[j].split("=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
  148. }
  149. if (qa[j].indexOf("#q=") > -1){
  150. var q0 = qa[j].split("#q=")[1].replace(/\s/g, "+").replace(/%22/g, '"');
  151. }
  152. }
  153. if (q0) return GCHSQTfH_cleanQuery(q0);
  154. else return "";
  155. }
  156. }
  157. }
  158. function GCHSQTfH_cleanQuery(s){
  159. var pos1 = s.indexOf('-"');
  160. var pos2, tmp = "", t2 = "", ret;
  161. while (pos1 > -1){
  162. pos2 = s.indexOf('"', pos1+2);
  163. if (pos2 == -1) break;
  164. if (pos2 > pos1){
  165. if (pos1 > 0) tmp = s.substr(0, pos1);
  166. if (pos2 < s.length-2){
  167. if (tmp.length>0) tmp += s.substr(pos2+1);
  168. else tmp += s.substr(pos2+2);
  169. }
  170. s = tmp;
  171. }
  172. pos1 = s.indexOf('-"');
  173. }
  174. var pos1 = s.indexOf('"');
  175. while (pos1 > -1){
  176. pos2 = s.indexOf('"', pos1+1);
  177. if (pos2 == -1) break;
  178. if (pos2 > pos1){
  179. if (pos1 > 0) tmp = s.substr(0, pos1);
  180. t2 = s.substr(pos1+1, pos2-pos1-1);
  181. if (oPrefs.phrases[0] == "Y"){
  182. t2 = t2.replace(/\+/g, "-"); //sticks phrase together
  183. }
  184. if (pos2 < s.length-2){
  185. if (tmp.length>0) tmp += t2 + "+" + s.substr(pos2+1);
  186. else tmp += t2 + "+" + s.substr(pos2+2);
  187. } else {
  188. tmp += t2;
  189. }
  190. s = tmp;
  191. }
  192. pos1 = s.indexOf('"');
  193. }
  194. s = s.replace(/"/g, "");
  195. var terms = s.replace(/\++/g, "+").split("+");
  196. for (var k=0; k<terms.length; k++){
  197. if (terms[k].indexOf("-")==0 || terms[k].indexOf("site:")==0 ||
  198. terms[k].indexOf("inurl:")==0 ||
  199. terms[k].toLowerCase()=="or" || terms[k]=="&") terms[k] = "";
  200. }
  201. while (terms[terms.length-1]=="") ret = terms.pop();
  202. s = terms.join("+");
  203. return s.replace(/\++/g, "+").replace("&", "%26").replace("#", "%23"); // 0.6.3 fix #
  204. }
  205. // Initial run to add event listeners
  206. GCHSQTfH_checkNode(document.body);
  207.  
  208. function openPrefs(e){
  209. var pform = document.getElementById("GCHSQTfH_pform");
  210. if (pform){
  211. pform.display = "block";
  212. } else {
  213. pform = document.createElement("div");
  214. pform.id = "GCHSQTfH_pform";
  215. pform.setAttribute("style", "position:fixed;bottom:0;background:#00f;color:#fff;font-weight:bold;font-size:20px;width:100%;z-index:999");
  216. pform.innerHTML= "<form onsubmit=\"return false;\"><button style=\"float:right\" id=\"btncacheClose\">X</button>" +
  217. "<label title=\"Switch between HTTP and HTTPS cache views\"><input " +
  218. "type=\"checkbox\" name=\"chkcacheSSL\" id=\"chkcacheSSL\"> " + oPrefs.ssl[1] + "</label><br>" +
  219. "<label title=\"Switch between phrases and separate words\"><input type=\"checkbox\" name=\"chkcachePhrases\" " +
  220. "id=\"chkcachePhrases\"> " + oPrefs.phrases[1] + "</label><br>" +
  221. "<label title=\"Convert action menu to inline display\"><input type=\"checkbox\" name=\"chkcacheActionStyle\" " +
  222. "id=\"chkcacheActionStyle\"> " + oPrefs.actionstyle[1] + " (reload after changing)</label></form>";
  223. document.body.appendChild(pform);
  224. }
  225. GCHSQTfH_fixssl();
  226. GCHSQTfH_fixphrases();
  227. GCHSQTfH_fixactionstyle();
  228. document.getElementById("btncacheClose").addEventListener("click", GCHSQTfH_closeprefs, false);
  229. document.getElementById("chkcacheSSL").addEventListener("change", GCHSQTfH_updtssl, false);
  230. document.getElementById("chkcachePhrases").addEventListener("change", GCHSQTfH_updtphrases, false);
  231. document.getElementById("chkcacheActionStyle").addEventListener("change", GCHSQTfH_updtactionstyle, false);
  232. }
  233. function GCHSQTfH_updtssl(e){ // Store setting for HTTP vs. HTTPS cached pages
  234. var chk = e.target;
  235. if (chk.checked){
  236. oPrefs.ssl[0] = "Y";
  237. } else {
  238. oPrefs.ssl[0] = "N";
  239. }
  240. GM_setValue("prefs", JSON.stringify(oPrefs));
  241. GCHSQTfH_fixssl();
  242. }
  243. function GCHSQTfH_fixssl(){ // Adjust checkbox for HTTP vs. HTTPS cached pages
  244. var chk = document.getElementById("chkcacheSSL");
  245. if (oPrefs.ssl[0] == "Y"){
  246. chk.setAttribute("checked","checked");
  247. chk.checked = true;
  248. } else {
  249. chk.removeAttribute("checked");
  250. chk.checked = false;
  251. }
  252. }
  253. function GCHSQTfH_updtphrases(e){ // Store setting for phrases vs individual words
  254. var chk = e.target;
  255. if (chk.checked){
  256. oPrefs.phrases[0] = "Y";
  257. } else {
  258. oPrefs.phrases[0] = "N";
  259. }
  260. GM_setValue("prefs", JSON.stringify(oPrefs));
  261. GCHSQTfH_fixphrases();
  262. }
  263. function GCHSQTfH_fixphrases(){ // Adjust checkbox for phrases vs individual words
  264. var chk = document.getElementById("chkcachePhrases");
  265. if (oPrefs.phrases[0] == "Y"){
  266. chk.setAttribute("checked","checked");
  267. chk.checked = true;
  268. } else {
  269. chk.removeAttribute("checked");
  270. chk.checked = false;
  271. }
  272. }
  273. function GCHSQTfH_updtactionstyle(e){ // Store setting for drop-down vs inline action links v0.6.4
  274. var chk = e.target;
  275. if (chk.checked){
  276. oPrefs.actionstyle[0] = "Y";
  277. } else {
  278. oPrefs.actionstyle[0] = "N";
  279. }
  280. GM_setValue("prefs", JSON.stringify(oPrefs));
  281. GCHSQTfH_fixactionstyle();
  282. }
  283. function GCHSQTfH_fixactionstyle(){ // Adjust checkbox for drop-down vs inline action links v0.6.4
  284. var chk = document.getElementById("chkcacheActionStyle");
  285. if (oPrefs.actionstyle[0] == "Y"){
  286. chk.setAttribute("checked","checked");
  287. chk.checked = true;
  288. } else {
  289. chk.removeAttribute("checked");
  290. chk.checked = false;
  291. }
  292. }
  293. function GCHSQTfH_closeprefs(e){
  294. e.target.parentNode.parentNode.style.display = "none";
  295. }
  296. function GCHSQTfH_convertPrefs(oDefault, oUser){ // Conform old objects to new defaults preserving values v0.6.4
  297. oDefault.ssl[0] = oUser.ssl[0];
  298. oDefault.phrases[0] = oUser.phrases[0];
  299. if (oUser.pref4){
  300. oDefault.actionstyle[0] = oUser.pref4[0];
  301. } else {
  302. oDefault.actionstyle[0] = oUser.actionstyle[0];
  303. }
  304. GM_setValue("prefs",JSON.stringify(oDefault));
  305. return oDefault;
  306. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址