xiefucai / RSS订阅

// ==UserScript==
// @id          RSS订阅
// @name        RSS订阅
// @description 找出网页的订源源,并生成订阅按钮,支持一键订阅到Feedly、Digg、InnoReader
// @namespace   http://www.xiefuai.com
// @include     *
// @creator     xiefucai [laozi12345@126.com]
// @copyright   xiefucai [laozi12345@126.com]
// @author      xiefucai [laozi12345@126.com]
// @version     1.0
// @date        2018-06-12
// @license     MIT
// ==/UserScript==

/*
 * Copyright (c) 2006-2014, Manpreet Singh [junkblocker@yahoo.com]
 *
 * 没啥好说的
 */

// ******
// Opera, Chrome, Chromium, Safari and IE7Pro compatible except
//
// 1) link tags injection does not work in IE7Pro
//
// due to non-firefox platform limitations/differences.
//
// IE7Pro bundles an older version of this script by default.
// ******


// Version 0.1 - First release
//             - 没啥好说的

(function (d) {
    var arr = [],
        types = ["application/rss+xml", "application/rsd+xml", "application/wlwmanifest+xml"],
        f = function (i, b) {
            return ['https://feedly.com/i/discover/sources/search/feed/', 'http://digg.com/reader/search/', 'http://www.inoreader.com/bookmarklet/subscribe/'][i] + encodeURIComponent(b.href);
        },
        getFeedContent = function (arr) {
            var s = ['<ul class="feed-list">'];
            arr.forEach(function (b) {
                s.push('<li><a href="' + b.href + '" target="_blank" class="rss-link">' + (b.title || d.title) + '</a> | <a href="' + f(0, b) + '" target="_blank" class="rss-link-feedly">订阅到Feedly</a> | <a href="' + f(1, b) + '" target="_blank" class="rss-link-digg">订阅到Digg</a> | <a href="' + f(2, b) + '" target="_blank" class="rss-link-inoreader">订阅到inoreader</a></li>')
            });
            s.push('<a href="javascript:;" class="close" onclick="this.parentNode.parentNode.classList.remove(\'open\')">&times;</a>');
            s.push('</ul>');
            return s.join("");
        },
        css = `
.feed {
    position: fixed;
    left:20px;
    bottom:20px;
    z-index: 9999;
}
.feed-button {
    display: block;
    width: 24px;
    height: 24px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='1024' height='1024'><path fill='#fff' d='M481.336889 918.243556C479.493689 712.362667 308.679111 542.850844 102.4 542.850844V542.72c-56.553244 0-102.4-45.846756-102.4-102.4s45.846756-102.4 102.4-102.4c322.349511 0 583.68 261.307733 583.68 583.68 0 56.553244-45.846756 102.4-102.4 102.4s-102.4-45.846756-102.4-102.4c0-1.120711 0.017067-2.241422 0.056889-3.356444zM819.2 921.6C819.2 526.364444 497.635556 204.8 102.4 204.8 45.846756 204.8 0 158.953244 0 102.4S45.846756 0 102.4 0c508.9792 0 921.6 412.6208 921.6 921.6 0 56.553244-45.846756 102.4-102.4 102.4s-102.4-45.846756-102.4-102.4z m-512-51.2c0 84.821333-68.778667 153.6-153.6 153.6S0 955.221333 0 870.4s68.778667-153.6 153.6-153.6 153.6 68.778667 153.6 153.6z'></path></svg>");
    background-color:#f50;
    background-size:70% 70%;
    background-repeat:no-repeat;
    background-position:50% 50%;
    color: #fff;
    border-radius:4px;
}
.feed-list{
    list-style: none;
    background: #fff;
    margin: 0;
    padding: 1em;
    border: 1px solid #ccc;
    box-shadow: 5px 5px 0px rgba(0,0,0,.05);
    display: none;
}
.feed-list li{
    border-bottom: 1px dotted #ccc;
    line-height: 3;
}
.feed-list li:last-child{
    border-bottom: none;
}
.feed-list .close{
    color: #999;
    position: absolute;
    top: 0.5em;
    right: 0.5em;
    font-size: 2em;
    text-decoration: none;
    line-height: 1;
    font-weight: bold;
}
.open .feed-list{
    display: block;
}
.open .feed-button{
    display: none;
}
.feed-list a{text-decoration: none;}
.feed-list a[class^='rss-link-']{
    display: inline-block;
    color: #fff;
    line-height: 1;
    font-size: 12px;
    border-radius: 3px;
    padding: 0.3em 0.7em;
}
.rss-link{
    color:#f50;
}
.rss-link-feedly{background-color:#2bb24c;}
.rss-link-digg{background-color:#333;}
.rss-link-inoreader{background-color: #167ac6;}
`
    ;

    Array.prototype.slice.call(d.getElementsByTagName('link')).forEach(function (link) {
        if (types.indexOf(link.type) >= 0) {
            arr.push(link);
        }
    });

    if (typeof GM_addStyle != "undefined") {
        GM_addStyle(css);
    } else if (typeof PRO_addStyle != "undefined") {
        PRO_addStyle(css);
    } else if (typeof addStyle != "undefined") {
        addStyle(css);
    } else {
        var node = document.createElement("style");
        node.type = "text/css";
        node.appendChild(document.createTextNode(css));
        var heads = document.getElementsByTagName("head");
        if (heads.length > 0) {
            heads[0].appendChild(node);
        } else {
            // no head yet, stick it whereever
            document.documentElement.appendChild(node);
        }
    }

    if (arr.length) {
        var feedLayer = d.createElement('div');
        feedLayer.className = 'feed';
        d.body.appendChild(feedLayer);
        feedLayer.innerHTML = `<a href="javascript:;" class="feed-button" title="订阅当前站点" onclick="this.parentNode.classList.add('open')"></a>` + getFeedContent(arr);
        /*
        var a = d.createElement('ul');
        d.body.appendChild(a);
        a.setAttribute("style", "border:1px solid #ccc;position:fixed;bottom:0;left:0;z-index:9999;background:#fff;padding:5px;font-size:12px;");
        a.innerHTML = ()(arr);
        d.getElementById('closerss').addEventListener('click', function (e) {
            d.body.removeChild(a);
        }, false);
        */
    }
})(document);