V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
autoxbc
V2EX  ›  分享发现

煎蛋网(jandan.net)补全原始题图

  •  1
     
  •   autoxbc · 2018-10-05 22:56:00 +08:00 · 1767 次点击
    这是一个创建于 2060 天前的主题,其中的信息可能已经有所发展或是发生改变。

    摸鱼网站煎蛋因为之前的图片版权风波,无法在文章中引用译介文章的原始题图,虽然通过合作图库配上了意思接近的图,离准确传达原文信息还是有些距离

    稍微研究了一下,发现来源网站的 Open Graph 信息都很完整,也就是源码中 meta[property="og:image"] 就包含题图,提取出来可以补全到文章中

    原图示例

    注意需要允许油猴跨域 Tampermonkey => 设置 => 安全 => @connect 模式 => 已禁用

    如果发现原图无法加载,有可能需要梯子或者伪造 Referrer

    // ==UserScript==
    // @name      煎蛋网(jandan.net)补全原始题图
    // @author      autoxbc
    // @version      1.0
    // @match      *://jandan.net/*
    // @grant      GM.xmlHttpRequest
    // ==/UserScript==
    
    const links = document.querySelectorAll('#content .post.f em a');
    [].slice.call(links).forEach( e => {
      const node = e.previousSibling ;
      if( !node || !node.data || node.data.trim() !== '本文译自' )
        return;
    
      GM.xmlHttpRequest( {
        method: 'GET',
        url: e.href ,
        onload: ( { response } ) => {
          const div = document.createElement('div');
          div.innerHTML = response ;
          const meta = div.querySelector('meta[property="og:image"]');
          if(!meta)
            return;
    
          let target ;
          const imgs = document.querySelectorAll('#content .post.f img');
          if( imgs.length === 1 )
          {
            target = imgs[0];
            const br = target.nextElementSibling ;
            if( br && br.matches('br') )
            {
              const em = br.nextElementSibling ;
              if( em && em.matches('em') )
                em.remove();
              br.remove();
            }
          }
          else {
            const h1 = document.querySelector('#content .post.f h1');
            target = h1.insertAdjacentElement('afterend', new Image() );
          }
    
          target.src = meta.content ;
          target.outerHTML += '<p style="text-align:center !important;">原图</p>';
        } ,
      } );
    } );
    
    5 条回复    2018-10-08 17:12:09 +08:00
    delectate
        1
    delectate  
       2018-10-06 07:02:07 +08:00
    需要配合 refcontrol 使用。
    silencefent
        2
    silencefent  
       2018-10-06 10:38:53 +08:00
    原先习惯了还没觉得...
    Thx4
        3
    Thx4  
       2018-10-06 18:47:14 +08:00
    左图笑死了哈哈哈哈哈
    xiaoxinshiwo
        4
    xiaoxinshiwo  
       2018-10-08 16:10:00 +08:00
    你是 cot 吗?
    autoxbc
        5
    autoxbc  
    OP
       2018-10-08 17:12:09 +08:00
    @xiaoxinshiwo #4 不是啦,我不穿女装的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   847 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 21:39 · PVG 05:39 · LAX 14:39 · JFK 17:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.