Yahoo!ニュースの元記事を探す

    
      
  • /*
     * @title Yahoo!ニュースの元記事を探す
     * @description Yahoo!ニュースでは元記事へのリンクがないので、元記事を探すのを補助するブックマークレットを作ってみた
     * @include https://news.yahoo.co.jp/articles/*
     * @include https://news.yahoo.co.jp/pickup/*
     * @license MIT License
     * @require 
     * @javascript_url
     */
    
    ( () => {
    'use strict';
    const
        yahoo_news_hostname = new URL( location.href ).hostname,
        cwin = window.open( 'about:blank' ),
        close_cwin = () => cwin.close(),
        goto_page = ( url ) => cwin.location.href = url,
        goto_search_page = ( search_info ) => goto_page( 'https://www.google.com/search?ie=UTF-8&q=' + encodeURIComponent( 'site:' + search_info.hostname + ' ' + search_info.keyword ) ),
        get_search_info = ( doc ) => {
            if ( ! doc ) doc = document;
            
            const
                site_url = ( doc.querySelector('a[data-ylk="rsec:detail;slk:banner;"]') || {} ).href;
            
            if ( ! site_url ) return null;
            
            return {
                hostname : new URL( site_url ).hostname,
                keyword : ( ( doc.querySelector( 'meta[property="og:title"]' ) || {} ).content || doc.title ).replace( /\s*-[^\-]*$/, '' ),
            };
        },
        search_info = get_search_info();
    
    if ( search_info !== null ) {
        goto_search_page( search_info );
        return;
    }
    
    const
        readmore_url = ( document.querySelector( '[data-ylk^="rsec:tpc_main;slk:headline;pos:"]' ) || {} ).href;
    
    if ( ! readmore_url ) {
        close_cwin();
        return;
    }
    
    goto_page( readmore_url );
    
    new Promise( resolve => {
        const
            check = () => {
                try {
                    if ( new URL( cwin.location.href ).hostname == yahoo_news_hostname ) {
                        resolve();
                        return;
                    }
                }
                catch ( error ) {
                }
                setTimeout( check, 100 );
            };
        
        check();
    } )
    .then( () => {
        const
            cdoc = cwin.document,
            do_search = () => {
                const
                    search_info = get_search_info( cdoc );
                
                if ( search_info !== null ) {
                    goto_search_page( search_info );
                    return;
                }
                close_cwin();
            };
        
        if ( cdoc.readyState === 'loading' ) {
            cdoc.addEventListener( 'DOMContentLoaded', do_search );
            return;
        }
        do_search();
    } );
    
    } )();
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2020/09/18 08:45:35 - 2020-09-18
  2. 2020/09/17 19:34:51 - 2020-09-17