stopGoogleRedirection

禁止google的搜索结果重定向,加快访问速度,防止撞墙(support (opera,firefox(GreaseMonkey),chrome) Latest Stable,IE9+)

  1. // ==UserScript==
  2. // @name stopGoogleRedirection
  3. // @author NLF
  4. // @description 禁止google的搜索结果重定向,加快访问速度,防止撞墙(support (opera,firefox(GreaseMonkey),chrome) Latest Stable,IE9+)
  5. // @version 1.0.0.5
  6. // @created 2013-12-26
  7. // @lastUpdated 2013-2-5
  8. // @grant none
  9. // @run-at document-start
  10. // @namespace http://userscripts.org/users/NLF
  11. // @homepage http://userscripts-mirror.org/scripts/show/186798
  12. // @include http*
  13. // @match *://*/*
  14. // ==/UserScript==
  15.  
  16. ;(function () {
  17. 'use strict';
  18.  
  19. // 将在真实环境下执行的实际函数。
  20. function contentScript() {
  21. 'use strict';
  22. // 匹配应用本脚本的网页
  23. if (!/^https?:\/\/www\.google(?:\.[A-z]{2,3}){1,2}\//i.test(location.href)) {
  24. return;
  25. };
  26. var emptyFn = function () {
  27. };
  28. // 覆盖google处理重定向的函数
  29. if (typeof Object.defineProperty == 'function') {
  30. Object.defineProperty(window, 'rwt', {
  31. configurable: false,
  32. enumerable: true,
  33. get: function () {
  34. return emptyFn;
  35. },
  36. });
  37. } else if (typeof window.__defineGetter__ == 'function') {
  38. window.__defineGetter__('rwt', function () {
  39. return emptyFn;
  40. });
  41. };
  42. };
  43.  
  44.  
  45. // 如果发生通信的话,需要一个独一无二的ID
  46. var messageID = Math.random().toString();
  47. // 把指定函数丢到真实环境中执行,规避一切脚本管理器乱七八糟的执行环境产生的奇葩Bug,
  48. // 特别是chrome上的那个坑爹tampermonkey。。。
  49. function runInPageContext(fn) {
  50. if (typeof fn !== 'function') {
  51. return;
  52. };
  53. // 创建一个脚本插入到pageContext执行
  54. var script = document.createElement('script');
  55. script.type = 'text/javascript';
  56. // 去掉函数名,防止污染全局环境。
  57. var sContent = ';(' + fn.toString().replace(/[^(]+/, 'function ') + ')(' + JSON.stringify(messageID) + ');';
  58. // console.log('执行的脚本实际内容\n', sContent);
  59. script.textContent = sContent;
  60.  
  61. // 检测html元素是否可访问
  62. // scriptish @run-at document-start时,html元素在第一时间不可访问
  63. var de = document.documentElement;
  64. if (de) {
  65. de.appendChild(script);
  66. de.removeChild(script);
  67. } else {
  68. new (window.MutationObserver || window.WebKitMutationObserver)(function (ms, observer) {
  69. var de = document.documentElement;
  70. if (de) {
  71. // console.log(de.outerHTML);
  72. observer.disconnect();
  73. de.appendChild(script);
  74. de.removeChild(script);
  75. };
  76. }).observe(document, {
  77. childList: true,
  78. });
  79. };
  80. };
  81.  
  82. runInPageContext(contentScript);
  83. })();