Guest User

No Google Search Redirect userscript

a guest
Sep 2nd, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. // ==UserScript==
  2. // @name No Google Search Redirect
  3. // @version 1.4
  4. // @author danei
  5. // @run-at document-start
  6. // @grant GM_addStyle
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @include http://www.google.*/*
  10. // @include https://www.google.*/*
  11. // @include http://ipv6.google.*/*
  12. // @include https://ipv6.google.*/*
  13. // @include http://encrypted.google.com/*
  14. // @include https://encrypted.google.com/*
  15. // @include http://news.google.*/*
  16. // @include https://news.google.*/*
  17. // @include http://maps.google.*/*
  18. // @include https://maps.google.*/*
  19. // @include http://ditu.google.*/*
  20. // @include https://ditu.google.*/*
  21. // @include http://mail.google.*/*
  22. // @include https://mail.google.*/*
  23. // ==/UserScript==
  24.  
  25. window.setTimeout(function()
  26. {
  27. GM_setValue('open_in_new_tab', false);
  28. }, 0);
  29.  
  30. function Hash(str) {
  31. var hash = 0,
  32. i, char;
  33. if (str.length == 0) return hash;
  34. for (i = 0; i < str.length; ++i) {
  35. char = str.charCodeAt(i);
  36. hash = ((hash << 5) - hash) + char;
  37. hash = hash & hash; // Convert to 32bit integer
  38. }
  39. return hash;
  40. }
  41. var Id2Funcs = {};
  42.  
  43. function DomNodeInserted(selector, func) {
  44. var id = 'nodeInserted' + Hash(selector);
  45. if (id in Id2Funcs) {
  46. ls = Id2Funcs[id].push(func);
  47. } else {
  48. var css = "@keyframes " + id + "{\
  49. from{clip: rect(1px, auto, auto, auto);}\
  50. to{clip: rect(0px, auto, auto, auto);}\
  51. }"
  52. GM_addStyle(css);
  53. css = selector + "{\
  54. animation-duration: 0.001s;\
  55. animation-name:" + id + ";\
  56. }";
  57. GM_addStyle(css);
  58. Id2Funcs[id] = [func];
  59. }
  60. }
  61. document.addEventListener('animationstart', function(evt) {
  62. if (evt.animationName in Id2Funcs) {
  63. var id = evt.animationName;
  64. for (var i = 0; i < Id2Funcs[id].length; ++i) {
  65. Id2Funcs[id][i](evt.target);
  66. }
  67. }
  68. }, false);
  69.  
  70. function endsWith(str, suffix) {
  71. return str && str.indexOf(suffix, str.length - suffix.length) !== -1;
  72. }
  73.  
  74. function startWith(str, prefix) {
  75. return str && str.indexOf(prefix) == 0;
  76. }
  77.  
  78. function contain(str, p) {
  79. return str && str.indexOf(p) !== -1;
  80. }
  81.  
  82. function open_in_new_tab(node) {
  83. window.setTimeout(function() {
  84. if (GM_getValue('open_in_new_tab')) {
  85. // console.log('in!',node.href);
  86. if (!node.hasAttribute('target')) node.setAttribute('target', '_blank');
  87. }
  88. }, 0);
  89. }
  90.  
  91. function splitURL(href) {
  92. var result = {};
  93. href.replace(/:\/\/([^\/?#]+)([^?#]*\/)([^?#]*)/, function(m, domain, path, file) {
  94. result.domain = domain, result.path = path, result.file = file;
  95. });
  96. return result;
  97. }
  98.  
  99. function getQueryParams(query) {
  100. var vars = {};
  101. query.replace(/[?&]+([^=&]+)=([^&]*)/g, function(m, key, value) {
  102. vars[key] = decodeURIComponent(value);
  103. });
  104. return vars;
  105. }
  106.  
  107. function trim_params(href) {
  108. return href.replace(/\?.+$/, '');
  109. }
  110.  
  111. function test_url(href) {
  112. return href && href.indexOf("://") !== -1;
  113. }
  114.  
  115. function stop(e) {
  116. e.stopPropagation();
  117. e.stopImmediatePropagation();
  118. }
  119.  
  120. function clear(node, domain_pattern, path_pattern, file_pattern, param_pattern) {
  121. if (node.getAttribute('onmousedown')) {
  122. node.removeAttribute('onmousedown');
  123. open_in_new_tab(node);
  124. }
  125. var k = splitURL(node.href);
  126. var domain = k.domain,
  127. path = k.path,
  128. file = k.file;
  129. if (domain && path && file && node.href && domain.match(domain_pattern) && path == path_pattern && file == file_pattern) {
  130. var realhref = getQueryParams(node.href)[param_pattern];
  131. if (realhref && test_url(realhref)) {
  132. //console.log(node.href);
  133. node.href = realhref;
  134. open_in_new_tab(node);
  135. }
  136. }
  137. }
  138.  
  139. function trim_search() {
  140. function run(node) {
  141. clear(node, /\.google\./, '/', 'url', 'url');
  142. clear(node, /\.google\./, '/', 'url', 'q');
  143. clear(node, /\.google\./, '/', 'interstitial', 'url');
  144. }
  145. document.addEventListener("DOMContentLoaded", function() {
  146. if (document.body && !document.body.hasAttribute("id")) {
  147. var list = document.querySelectorAll('body a');
  148. for (var i = 0; i < list.length; ++i) {
  149. run(list[i]);
  150. }
  151. }
  152. }, false);
  153. DomNodeInserted("#ires a,#rhs_block a", run);
  154. }
  155. var pic_map = {};
  156.  
  157. function trim_image() {
  158. var run = function(node) {
  159. var h = node.href;
  160. if (h) {
  161. var k = getQueryParams(h);
  162. var orihref = k.imgurl,
  163. refhref = k.imgrefurl;
  164. if (orihref && refhref) pic_map[orihref] = refhref;
  165. }
  166. clear(node, /\.google\./, '/', 'imgres', 'imgurl');
  167. }
  168. document.addEventListener("DOMContentLoaded", function() {
  169. if (document.body && !document.body.hasAttribute("id")) {
  170. var list = document.querySelectorAll('body a');
  171. for (var i = 0; i < list.length; ++i) {
  172. run(list[i]);
  173. }
  174. }
  175. }, false);
  176. var clear_trim = function(event) {
  177. var node = event.target;
  178. if (event.attrName == 'href') node.href = trim_params(node.href);
  179. }
  180. DomNodeInserted('#rg_hta,#rg_hl', function(node) {
  181. node.addEventListener('DOMAttrModified', clear_trim, false);
  182. });
  183. // mordern 2
  184.  
  185. function fix(node) {
  186. // console.log(node.href,'fixed');
  187. clear(node, /\.google\./, '/', 'url', 'url');
  188. clear(node, /\.google\./, '/', 'imgres', 'imgurl');
  189. if (node.hasAttribute('replaced')) return;
  190. var newnode = node.cloneNode(true);
  191. open_in_new_tab(newnode);
  192. // if(/*GM_getValue('open_in_new_tab') &&*/ !newnode.hasAttribute('target'))newnode.setAttribute('target','_blank')
  193. // console.log(node.href,'replaced!');
  194. newnode.addEventListener("click", stop);
  195. newnode.addEventListener("mousedown", stop);
  196. newnode.addEventListener("keydown", stop);
  197. newnode.addEventListener("blur", stop);
  198. newnode.addEventListener('DOMAttrModified', function() {
  199. // console.log(this.href,'modified!');
  200. fix(this);
  201. })
  202. newnode.setAttribute('replaced', '1');
  203. node.parentNode.replaceChild(newnode, node);
  204. }
  205. DomNodeInserted("#irc_mil,#irc_itl,#irc_hol,#irc_vpl,#irc_fsl,#ires a.irc_fsl,#ires a.irc_vpl,#ires a.irc_itl", fix);
  206. }
  207.  
  208. function add_link() {
  209. var parser_link = function(attachnode, orinode) {
  210. var n = attachnode.getElementsByTagName('a');
  211. if (n.length == 0) {
  212. var url = attachnode.innerHTML;
  213. attachnode.innerHTML = '<a>' + url + '</a>';
  214. open_in_new_tab(attachnode);
  215. }
  216. var r = getQueryParams(orinode.href).imgurl || orinode.href;
  217. var refhref = (r && pic_map[r]) || ('http://' + n[0].innerHTML);
  218. attachnode.getElementsByTagName('a')[0].href = refhref;
  219. }
  220. document.addEventListener("DOMContentLoaded", function() {
  221. var classic_pic_url = document.querySelectorAll('a+font>font')
  222. for (var i = 0; i < classic_pic_url.length; ++i) {
  223. var n = classic_pic_url[i];
  224. var ahref = n.parentNode.parentNode.firstChild;
  225. if (ahref.tagName == 'A') parser_link(n, ahref);
  226. }
  227. var classic_pic_url = document.querySelectorAll('#ires cite')
  228. for (var i = 0; i < classic_pic_url.length; ++i) {
  229. var n = classic_pic_url[i];
  230. var ahref = n.parentNode.firstChild;
  231. if (ahref.tagName == 'A') parser_link(n, ahref);
  232. }
  233. }, false);
  234. DomNodeInserted('#rg_hr', function() {
  235. var highlight = document.getElementById('rg_hr'),
  236. pop = document.getElementById('rg_hta');
  237. if (highlight && pop && !highlight.onmouseover) {
  238. highlight.onmouseover = function() {
  239. parser_link(highlight, pop);
  240. highlight.onmouseover = null;
  241. };
  242. }
  243. });
  244. }
  245.  
  246. function trim_news() {
  247. if (window.location.hostname.match(/news\.google\./) || (window.location.hostname.match(/\.google\./) && startWith(window.location.pathname, '/news'))) {
  248. var run = function(node) {
  249. if (node.getAttribute("originalhref")) {
  250. node.href = node.getAttribute("originalhref");
  251. }
  252. }
  253. DomNodeInserted('a', function(node) {
  254. if (test_url(node.href)) {
  255. run(node);
  256. node.addEventListener("click", stop);
  257. node.addEventListener("DOMAttrModified", function() {
  258. run(this);
  259. });
  260. }
  261. });
  262. }
  263. }
  264.  
  265. function trim_mail() {
  266. if (window.location.hostname.match(/mail\.google\./)) {
  267. document.addEventListener("DOMContentLoaded", function() {
  268. var list = document.querySelectorAll(".msg a");
  269. for (var i = list.length - 1; i >= 0; i--) {
  270. clear(list[i], /\.google\./, '/', 'url', 'q');
  271. }
  272. }, false);
  273.  
  274. DomNodeInserted('.h7.ie.nH.oy8Mbf a', function(node) {
  275. if (test_url(node.href)) {
  276. node.addEventListener("click", stop);
  277. }
  278. });
  279. }
  280. }
  281.  
  282. function trim_map() {
  283. if (window.location.hostname.match(/maps|ditu\.google\./)) {
  284. DomNodeInserted('#page a', function(node) {
  285. clear(node, /maps|ditu\.google\./, '/', 'local_url', 'q');
  286. clear(node, /maps|ditu\.google\./, '/', 'aclk', 'adurl');
  287. });
  288. }
  289. }
  290. trim_search();
  291. trim_image();
  292. add_link();
  293. trim_map();
  294. trim_news();
  295. trim_mail();
Add Comment
Please, Sign In to add comment