realAzazello

Wayback Google v0.6 userscript by zanetu

Oct 6th, 2020 (edited)
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Wayback Google
  3. // @version      0.6
  4. // @author       zanetu
  5. // @namespace    http://userscripts.org/users/92143
  6. // @description  Adds WayBackMachine (archive.org) links to Google search results and automatically redirects to WayBackMachine when the Google "Cached" link fails (e.g. 404 error).
  7. // @include      /^https?://www\.google\./
  8. // @include      /^https?://webcache\.googleusercontent\./
  9. // @license      GPL version 2 or any later version; http://www.gnu.org/licenses/gpl-2.0.txt
  10. // @require      http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
  11. // @supportURL  https://github.com/zanetu
  12. // @attribution changes [d:10.06.20][u:<ul><li>added...@supportURL</li></ul>]
  13. // @attribution changes [d:10.04.20][u:<ul><li>Discussion on updating script: https://greasyfork.org/en/discussions/greasyfork/63969</li></ul>]
  14. // @grant        GM_addStyle
  15. // @run-at       document-end
  16. // ==/UserScript==
  17.  
  18. //google search
  19. if('webcache' != location.hostname.split('.')[0]) {
  20.     //do not run in frames or iframes
  21.     if (window.top == window.self) {
  22.         function modifyGoogle() {
  23.             //add wayback machine archive links
  24.             $('cite').each(function() {
  25.                 //legacy, esp. news
  26.                 var $firstA = $(this).parent().prev('a')
  27.                 if(!$firstA.length) {
  28.                     //legacy, esp. maps
  29.                     $firstA = $(this).prev().children('a:first-child')
  30.                 }
  31.                 if(!$firstA.length) {
  32.                     $firstA = $(this).closest('.g').find('a:first')
  33.                 }
  34.                 if($firstA.length && !$firstA.hasClass('wayback')) {
  35.                     $firstA.addClass('wayback')
  36.                     var $container = $(this).parent()
  37.                     var leftMargin = '0px'
  38.                     if('crc' == $container.children(':last').attr('class')) {
  39.                         leftMargin = '16px'
  40.                     }
  41.                     $container
  42.                     .append(' ')
  43.                     .append(
  44.                         $('<a/>')
  45. // Azazello - change 'http' to 'https'
  46.                         .attr('href', 'https://web.archive.org/web/' + $firstA.attr('href'))
  47.                         .html('Wayback').attr('class', 'fl _Zkb wayback')
  48.                         .css({'margin-left':leftMargin, 'display':'inline'})
  49.                     )
  50.                     .append(' ')
  51.                 }
  52.             })
  53.         }
  54.         //prevent knowledge graph from disabling wayback links
  55.         GM_addStyle('#center_col {z-index:1;}')
  56.         MutationObserver = window.MutationObserver || window.WebKitMutationObserver
  57.         if(MutationObserver) {
  58.             var observer = new MutationObserver(function(mutations) {
  59.                 modifyGoogle()
  60.             })
  61.             //tiny delay needed for firefox
  62.             setTimeout(function() {
  63.                 if(1 == $('#main').length) {
  64.                     observer.observe($('#main').get(0), {
  65.                         childList: true,
  66.                         subtree: true
  67.                     })
  68.                 }
  69.                 modifyGoogle()
  70.             }, 100)
  71.         }
  72.         //for chrome v18-, firefox v14-, internet explorer v11-, opera v15- and safari v6-
  73.         else {
  74.             setInterval(function() {
  75.                 modifyGoogle()
  76.             }, 500)
  77.         }
  78.     }
  79. }
  80. //google cache
  81. else {
  82.     //cache miss
  83.     if(1 === $('code:contains("/search?q=cache:")').length) {
  84.         var link = decodeURIComponent(/\=cache\:.*?\:([^\+]+)/.exec(location.search)[1] || '')
  85. // Azazello - change 'http' to 'https'
  86.         link && location.replace('https://web.archive.org/web/' + link)
  87.     }
  88. }
Add Comment
Please, Sign In to add comment