V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
bramblex
V2EX  ›  JavaScript

刚刚随手写了一个猴子脚本,把 google 网页快照里面的所有链接全部转换成网页快照的链接

  •  
  •   bramblex · 2015-07-27 12:37:12 +08:00 · 3065 次点击
    这是一个创建于 3196 天前的主题,其中的信息可能已经有所发展或是发生改变。

    不废话,上代码。依赖Jquery

    $('#content a[href]').each(function () {
      $this = $(this);
      var href = this.href;
      $this.attr('href', 'http://webcache.googleusercontent.com/search?q=cache:' + href);
    });
    
    9 条回复    2015-07-28 19:23:37 +08:00
    pengfei
        1
    pengfei  
       2015-07-27 12:41:05 +08:00
    不懂,怎么用
    xiandao7997
        2
    xiandao7997  
       2015-07-27 12:46:56 +08:00
    @pengfei 火狐搜 GreaseMonkey, Chrome 搜 TamperMonkey
    KCheshireCat
        3
    KCheshireCat  
       2015-07-27 12:50:57 +08:00
    Array.prototype.slice.call(document.querySelectorAll("a[href]")).forEach(function(element, index, array) {element.href = 'http://webcache.googleusercontent.com/search?q=cache:' + element.href;});

    改版为无依赖
    oott123
        4
    oott123  
       2015-07-27 13:04:19 +08:00 via Android
    @KCheshireCat 为啥不 Array.prototype.forEach.call 呢(
    KCheshireCat
        5
    KCheshireCat  
       2015-07-27 13:09:28 +08:00
    @oott123 因为我没系统了解过这些东西,都是遇到问题,寻找解决方案这样的。。。。

    Array.prototype.forEach.call(document.querySelectorAll("a[href]"), function(element, index, array) {element.href = 'http://webcache.googleusercontent.com/search?q=cache:' + element.href;});
    bramblex
        6
    bramblex  
    OP
       2015-07-27 13:33:33 +08:00
    @KCheshireCat

    不错。/w\
    yanwen
        7
    yanwen  
       2015-07-28 17:45:03 +08:00
    求个完整版的 user.js 脚本。谢谢。。
    bramblex
        9
    bramblex  
    OP
       2015-07-28 19:23:37 +08:00
    @KCheshireCat

    还是有依赖,不过这次用的方式更聪明了……不直接改链接了,而是劫持链接。虽然这样做有个问题,就是如果右键打开的话就会出问题了……

    https://greasyfork.org/zh-CN/scripts/11281-%E5%BF%AB%E7%85%A7%E9%93%BE%E6%8E%A5

    // ==UserScript==
    // @name 快照链接
    // @namespace lovearia.me
    // @description 把谷歌快照的链接全部转换成谷歌快照
    // @include http://webcache.googleusercontent.com/search?*q=cache:*
    // @version 2
    // @grant none
    // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
    // ==/UserScript==

    $('body').on('click', 'a[href]', function (e) {
    if (!$(this).closest('#google-cache-hdr').length) {
    var href = 'http://webcache.googleusercontent.com/search?q=cache:' + escape(this.href);
    location.href = href;
    e.preventDefault();
    }
    });
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   957 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 557ms · UTC 20:05 · PVG 04:05 · LAX 13:05 · JFK 16:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.