Google news API endpoint generator lite

  • /*
     * @title Google news API endpoint generator lite
     * @description グーグルのニュースを検索して結果をJSONで受け取ることができます。【確認用】
     * @license MIT License
     * @require 
    
    memo
    1. YQLを利用しています。何かと都合がいいです。
    2. 対象となる記事は日本語に限定されません。optionで選択できるようにするといいかも。
    3. DOM未確認のまま操作しており、空白のページ他では適切に動作しないことがあります。
    4. 右下のPackedをクリックしてコードをコピー!
    
    */
    
    (function () {
        var createPlaceholder = function(){
            //overlay
            var overlay = document.createElement('div');
                overlay.style.width = '100vw';
                overlay.style.height = '100vh';
                overlay.style.position = 'absolute';
                overlay.style.top = '0';
                overlay.style.left = '0';
                overlay.style.backgroundColor = 'rgba(0, 0, 0, 0.5)';
                overlay.style.zIndex = 2121212120;
            document.body.appendChild(overlay);
    
            //main
            var c = document.createElement('div');
                c.style.padding = '10px 20px';
                c.style.width = '95vw';
                c.style.position = 'absolute';
                c.style.top = '30px';
                c.style.left = '30px';
                c.style.backgroundColor = '#f0f0f0';
                c.style.zIndex = 2121212121;
                c.innerHTML = '<form name="form_x">' + 
                    '<h1>Google news API endpoint generator lite</h1>' +
                    '<h2>keyword</h2>'+
                    '<label><input type="textbox" value="はてな"></label>' +
                    '<h2>limit</h2>' +
                    '<label><input type="textbox" value="15"></label>' +
                    '<h2>channel</h2>' +
                    '<label><input type="checkbox" value="title" checked="checked">title</label> ' +
                    '<label><input type="checkbox" value="link" checked="checked">link</label> ' +
                    '<label><input type="checkbox" value="category">category</label> ' +
                    '<label><input type="checkbox" value="pubDate" checked="checked">pubDate</label> ' +
                    '<label><input type="checkbox" value="description" checked="checked">description</label> ' +
                    '<div><button type="button" onclick="test()" style="margin:1em 0; padding:0.3em; font-size:13pt">Test</button></div>' +
                    '</form>' +
                    '<div id="result" style="background:#ddd;padding:0.5em;">[Endpoint]</div>';
            document.body.appendChild(c);
            document.getElementById("result").style.wordBreak = 'break-all'; 
            
            //click overlay to remove all
            overlay.onclick = function(){
                document.body.removeChild(c);
                document.body.removeChild(overlay);
            };
        };
        createPlaceholder();
    }());
    
    var test = function(){
        //keyword
        var keywords = document.form_x.elements[0].value;
        
        //limit
        var limit = document.form_x.elements[1].value;
        if(limit>15 || limit<0){
            limit = 15;
        }
    
        //channel
        var item = [];
        for(i=2; i<7; i++){
            if( document.form_x.elements[i].checked ){
                item.push(document.form_x.elements[i].value);
            }
        }
        if(item.length < 1 || item.length > 4){//0 or 5 -> wildcard
            item = ['*'];
        }
    
        //result
        var endPoint = 'https://query.yahooapis.com/v1/public/yql?q=select' + 
            encodeURI(' ' + item + ' from feed where url=' + 
            '"https://news.google.com/news/rss/search/section/q/' + encodeURI(keywords) + 
            '" limit ') + limit + 
            "&format=json";
    
        document.querySelector('#result').textContent = endPoint;
        console.log(endPoint);
    }
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2017/09/28 19:54:09 - 2017-09-28