// ==UserScript==
// @name YouTubeとニコニコ動画にRSSリンクを追加
// @description YouTube:ユーザー、マイリスト、チャンネル ニコニコ動画:ユーザー、マイリスト、タグ reddit:キーワード検索結果、サブレディット、スレッド twitter:キーワード検索、ユーザー Pinterest:ユーザー、ボード
// @version 0.5.1
// @run-at document-idle
// @match *://www.youtube.com
// @match *://www.youtube.com/*
// @match *://www.nicovideo.jp/user/*
// @match *://www.nicovideo.jp/mylist/*
// @match *://www.nicovideo.jp/tag/*
// @match *://www.nicovideo.jp/related_tag/*
// @match *://www.nicovideo.jp/search/*
// @match *://www.nicovideo.jp/watch/*
// @match *://www.nicovideo.jp/mylist_search/*
// @match *://www.reddit.com/*
// @match *://twitter.com/*
// @match *://www.pinterest.jp/*
// @grant none
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @namespace https://greasyfork.org/users/181558
// @downloadURL none
// ==/UserScript==
(function() {
const INSURE = 0;
if (location.href.match(/\/\/twitter\.com\//)) twitterRSS();
else if (location.href.match(/\/\/www\.youtube\.com|\/\/www\.nicovideo\.jp\/|\/\/www\.reddit\.com\/|\/\/www\.pinterest\./)) youtubeNicoRedditRSS();
return;
// ---------------------------------------------------------------------------------------------------------------------
function youtubeNicoRedditRSS() {
(function() {
var rssICON = '
';
addLinks();
setTimeout(addLinks, 200);
//ページ継ぎ足しアドオンに対応
if (location.href.match(/related_tag|mylist_search/)) document.addEventListener("AutoPagerize_DOMNodeInserted", addLinks);
//AutoPagerize系アドオンでuserやタグページを継ぎ足した時に2ページ目以降のリンクがおかしくなるのを修正
//userページ
if (location.href.match(/www.nicovideo.jp\/user/)) document.addEventListener("AutoPagerize_DOMNodeInserted", function() {
for (let ele of elegeta("//div[@class='section']/h5/a[contains(@href,'https://www.nicovideo.jp/user/')]"))
ele.href = "https://www.nicovideo.jp/watch/" + ele.href.replace(/https?:\/\/www.nicovideo.jp\/user\/\d*\/watch\//, "");
});
//related_tagページ
if (location.href.match(/www.nicovideo.jp\/related_tag/)) document.addEventListener("AutoPagerize_DOMNodeInserted", function() {
for (let ele of elegeta("//a[contains(@href,'https://www.nicovideo.jp/related_tag/tag/')]"))
ele.href = "https://www.nicovideo.jp/tag/" + ele.href.replace(/https?:\/\/www.nicovideo.jp\/related_tag\/tag\//, "");
});
if (!observer) {
var href = location.href;
var observer = new MutationObserver(function(mutations) {
if (href !== location.href) {
href = location.href;
removeAllRSS();
for (let i = 0; i < (location.href.match(/youtube|pinterest/) ? 4 : 1 + INSURE); i++) setTimeout(redditYoutubeInitAndAddRSS, 1333 + i * 1000);
}
});
observer.observe(document, { childList: true, subtree: true });
}
function removeAllRSS() { // 前回つけたRSSを剥がす
$('.multirss').remove();
}
function redditYoutubeInitAndAddRSS() {
if (location.href.match(/^https?:\/\/www\.reddit\.com\/search\/?\?q=/gmi) && eleget0('//input[@id="header-search-bar"]')) { // reddit search
var url = "https://www.reddit.com/search.rss?q=***&sort=new".replace("***", eleget0('//div[1]/div/h3/span').innerText);
addICONandRAD(eleget0('//div[1]/div/h3/span'), url, eleget0('//div[1]/div/h3/span').value);
} else
if (location.href.match(/^https?:\/\/www\.reddit\.com\/r\//gmi) && location.href.match(/search|comments/) == null) { // reddit subreddit
var url = location.href + (location.href.match(/\/$/) ? ".rss" : "/.rss");
addICONandRAD(eleget0('//div[@role="navigation"]/button'), url);
} else
if (location.href.match(/^https?:\/\/www\.reddit\.com\/.+/gmi) && location.href.match(/\/comments\//)) { // reddit thread
var url = location.href + (location.href.match(/\/$/) ? ".rss" : "/.rss");
addICONandRAD(eleget0('//div/div[3]/div[1]/div/h1|//div[2]/div/div[2]/div/div/h1'), url);
}
addRSSICONandRADlink("://www.youtube.com/playlist?list=", '//yt-formatted-string[@class="style-scope ytd-playlist-sidebar-primary-info-renderer"]/a|//h1[@class="style-scope ytd-playlist-sidebar-primary-info-renderer"]/yt-formatted-string', '*RSS Feed*', "list=");
addRSSICONandRADlink("://www.youtube.com/channel/", '//div[@id="meta"]/ytd-channel-name[@id="channel-name"]|//span[@id="channel-title"]', '*RSS Feed*', "channel/");
addRSSICONandRADlink("://www.youtube.com/user/", '//div[@id="meta"]/ytd-channel-name|//span[@id="channel-title"]', '*RSS Feed*', "user/");
// Pinterest ボード
if (location.href.match(/\/\/www\.pinterest\.jp\/[^/]*\/[^/]*\//) && !(location.href.match(/\/\/www\.pinterest\.jp\/pin\//))) {
var url = location.href.match(/https:\/\/www\.pinterest\.jp\/[^/]*\/[^/]*/) + ".rss";
var place = eleget0('//DIV[1]/DIV[1]/H1[1]|//div[last()]/div/div/div/div[1]/h4'); //TBD
addICONandRAD(place, url, place.value);
}
// Pinterest ユーザー
if (location.href.match(/\/\/www\.pinterest\.jp\/[^/]*\/$/)) {
setTimeout(() => {
var url = location.href.match(/https:\/\/www\.pinterest\.jp\/[^/]*\/$/) + "feed.rss";
var place = eleget0('/HTML/BODY/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[1]/DIV[2]/DIV[1]/DIV[1]/DIV[1]/DIV[2]/DIV[1]/DIV[1]/H1[1]|//div/div/div/div[1]/h4|//div[@style="margin-top: 20px;"]/div/h5'); //TBD o?
addICONandRAD(place, url, place.value);
}, 2200);
}
}
return;
function addICONandRAD(place, url, title = "") {
if (!place) return;
removeAllRSS();
var link = document.body.parentNode.insertBefore(document.createElement("link"), document.body);
link.title = title || place.innerText;
link.rel = "alternate";
link.type = "application/rss+xml";
link.className = "multirss";
link.href = url;
if (!link.title) return;
var url = link.href;
var ele = place.appendChild(document.createElement('span'));
ele.setAttribute("style", "font-weight:normal;");
ele.className = "multirss";
let rssICON = '
';
ele.innerHTML = " " + rssICON + "";
}
//実際にリンクを付ける
function addLinks() {
//「新しいタブで開く」指定を外す
for (let ele of elegeta('//a[@target="_blank"]')) ele.removeAttribute("target");
removeAllRSS();
redditYoutubeInitAndAddRSS();
var nicodeleteOpt = /[\?&]f_range=.|[\?&]l_range=.|[\?&]opt_md=.*|[\?&]start=.*|[\?&]end=.*|[\?&]ref=[^&]*|[\?&]sort=.|[\?&]order=.|[\?&]page=\d*|[\?&]track=[^&]*|#+.*/g;
addRSSICONandRADlink("www.nicovideo.jp/user/", '//div[@class="profile"]', '*RSS Feed*', "/user/", "", nicodeleteOpt);
addRSSICONandRADlink("www.nicovideo.jp/mylist/", '//div[@id="SYS_box_mylist_header"]/div/h1', '*RSS Feed*', "", "?rss=2.0&lang=ja-jp&special_chars_decode=1", nicodeleteOpt);
addRSSICONandRADlink("www.nicovideo.jp/tag/", '//header[@class="contentHeader"]/h1/span', '*RSS Feed*', "", "?sort=f&rss=2.0&lang=ja-jp", nicodeleteOpt);
addRSSICONandRADlink2("www.nicovideo.jp/related_tag/", '//table[@class="font12"]/tbody/tr/td/a', '*RSS Feed*', "", "", nicodeleteOpt);
addRSSICONandRADlink2("www.nicovideo.jp/search", "//ul[@class='tags']//a", '*RSS Feed*', "", "", nicodeleteOpt);
addRSSICONandRADlink2("www.nicovideo.jp/tag", "//ul[@class='tags']//a", '*RSS Feed*', "", "", nicodeleteOpt);
addRSSICONandRADlink2("www.nicovideo.jp/watch/", '//a[@class="Link TagItem-name"]', '*RSS Feed*', "", "", nicodeleteOpt);
if (/www.nicovideo.jp\/mylist_search\//.exec(location.href))
for (let ele of elegeta('//div[@id="PAGEBODY"]/div[2]/div/div/p/a')) addRSSICONandRAD(ele, ele.href + "?rss=2.0&lang=ja-jp&special_chars_decode=1", ele.innerText);
return;
}
function eleget0(xpath) {
var ele = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
return ele.snapshotLength > 0 ? ele.snapshotItem(0) : "";
}
function elegeta(xpath) {
var ele = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var array = [];
for (var i = 0; i < ele.snapshotLength; i++) array[i] = ele.snapshotItem(i);
return array;
}
function addRAD(inurl, xpath, newurl, part, after, urlGarbage = /$^/) { // RSS auto-discoveryを付ける
if (location.href.indexOf(inurl) == -1) return
var place = eleget0(xpath);
var link = eleget0('//head').appendChild(document.createElement("link"));
var url2 = after ? ('*RSS Feed*') : newurl.replace("***", location.href.split(part)[1].match(/[^\?&\/]*/g)[0]);;
link.rel = "alternate"
link.type = "application/rss+xml"
link.title = document.title;
link.href = url2;
link.setAttribute("class", "multirss");
}
function addRSSICONandRAD(place, url, title) {
// removeAllRSS();
var link = place.parentNode.insertBefore(document.createElement("a"), place);
link.href = url;
link.setAttribute("title", title + "のRSSを購読");
link.setAttribute("class", "multirss");
link.innerHTML = rssICON;
var rac = document.createElement("link");
rac.rel = "alternate"
rac.type = "application/rss+xml"
rac.title = title;
rac.href = url
rac.setAttribute("class", "multirss");
eleget0('//head').appendChild(rac);
}
function addRSSICONandRADlink(inurl, xpath, newurl, part, after, urlGarbage = /$^/) {
if (location.href.indexOf(inurl) == -1) return;
var ele = eleget0(xpath);
if (!ele) return;
removeAllRSS();
var text = newurl + location.href.split(part)[1];
var url = location.href.replace(urlGarbage, "");
var link = document.createElement("span");
var url2 = after ? ('*RSS Feed*') : newurl.replace("***", location.href.split(part)[1].match(/[^\?&\/]*/g)[0]);
var bgcol = "background-color:#c9e9ff;";
link.setAttribute("title", ele.innerText + "のRSSを購読");
link.setAttribute("class", "multirss");
link.innerHTML = url2.replace("*RSS Feed*", rssICON)
ele.parentNode.appendChild(link);
addRAD1(ele, url2);
return;
}
function addRSSICONandRADlink2(inurl, xpath, newurl, part, after, urlGarbage = /$^/) {
if (location.href.indexOf(inurl) == -1) return
removeAllRSS();
for (let ele of elegeta(xpath)) {
var url = location.href.replace(urlGarbage, "");
var link = document.createElement("span");
var url2 = newurl.replace("***", encodeURIComponent(ele.innerText));
var bgcol = "background-color:#c9e9ff;";
link.setAttribute("title", ele.innerText + "のRSSを購読");
link.setAttribute("class", "multirss");
link.innerHTML = url2.replace("*RSS Feed*", rssICON)
ele.parentNode.insertBefore(link, ele);
addRAD1(ele, url2)
}
return;
}
function addRAD1(ele, url2) {
// removeAllRSS();
for (let rad of elegeta("//link[@type='application/rss+xml']")) { if (rad.href == url2.match(/\"(.*)\"/)[1]) return; } // 重複登録を避ける
var rac = document.createElement("link");
rac.rel = "alternate"
rac.type = "application/rss+xml"
rac.title = ele.innerText;
rac.href = url2.match(/\"(.*)\"/)[1];
rac.setAttribute("class", "multirss");
eleget0('//head').appendChild(rac);
}
})();
}
// ---------------------------------------------------------------------------------------------------------------------
function twitterRSS() {
(function() {
addAutoDiscovery();
setTimeout(addAutoDiscovery, 2000); //addAutoDiscovery();
if (location.href.match(/twitter/)) {
document.addEventListener('keydown', function(e) {
if (e.key == "Enter") setTimeout(addAutoDiscovery, 2000);
}, false);
document.addEventListener('click', function(e) {
setTimeout(addAutoDiscovery, 2000);
}, false);
}
return;
function addAutoDiscovery() { // Twitter検索へのRSSリンクとRSS Autodiscoveryを埋め込む
for (let ele of elegeta('//*[@class="multirss"]')) ele.remove();
embedAutoDiscovery('//div[@class="ProfileHeaderCard"]/h2/a/span', /(^@)/, "from:@", "from:*** | Queryfeed Twitterユーザー", "https://Queryfeed.net/tw?q=***")
embedAutoDiscovery('//div[@class="ProfileHeaderCard"]/h2/a/span', /(^@)/, "from:@", "from:*** (filter:images OR filter:videos) | Queryfeed Twitterユーザー 画像か動画", "https://Queryfeed.net/tw?q=***%20 (filter:images OR filter:videos)")
embedAutoDiscovery('//div[@class="ProfileHeaderCard"]/h2/a/span', /(^@)/, "from:@", "from:*** (filter:videos) | Queryfeed Twitterユーザー 動画", "https://Queryfeed.net/tw?q=***%20 (filter:videos)")
embedAutoDiscovery('//h1[@class="SearchNavigation-titleText"]', /(.)/, "$1", "*** | Queryfeed Twitterキーワード", "https://Queryfeed.net/tw?q=***")
embedAutoDiscovery('//h1[@class="SearchNavigation-titleText"]', /(.)/, "$1", "*** | Queryfeed Twitterキーワード 画像か動画", "https://Queryfeed.net/tw?q=***%20 (filter:images OR filter:videos)")
embedAutoDiscovery('//h1[@class="SearchNavigation-titleText"]', /(.)/, "$1", "*** | Queryfeed Twitterキーワード 動画", "https://Queryfeed.net/tw?q=***%20 (filter:videos)")
return;
}
function embedAutoDiscovery(wordXP, wordtermRE, replaceStr, itemName, url) {
// if (location.href.indexOf("google") == -1) return
var ele = eleget0(wordXP);
if (!ele || !(ele.innerText.match(wordtermRE))) return;
var word = ele.innerText;
// if (itemName.match(/\(En\)/) && !(word.match(/^[\x20-\x7e]*$/))) return; // google news en なら半角英数以外を含んだらやらない
var link = eleget0('//head').appendChild(document.createElement("link"));
link.title = itemName.replace("***", word);
link.rel = "alternate"
link.type = "application/rss+xml"
link.className = "multirss";
link.href = url.replace("***", encodeURIComponent(word.replace(wordtermRE, replaceStr)));
var place = eleget0('//div/h2/a/span[@class="username u-dir"]|//h1[@class="SearchNavigation-titleText"]');
if (!place) return;
var url = link.href;
var ele = place.appendChild(document.createElement('span'));
ele.className = "multirss";
ele.setAttribute("style", "font-weight:normal;");
let rssICON = '
';
ele.innerHTML = " " + rssICON + "";
return;
}
function addLink(site, placexpath, terms, beforetitle, title, append, deleteoption, option) {
if (location.href.indexOf(site) == -1) return;
var place = eleget0(placexpath);
if (!place) return;
var url = window.location.href;
if (terms !== "") url = url.replace(/&tbm=.*/, "");
if (terms !== "none") url = url + terms;
url = url.replace(deleteoption, '') + option;
var ele = document.createElement('span');
ele.className = "multirss";
ele.setAttribute("style", "font-weight:normal;");
ele.innerHTML = beforetitle + "" + title + "" + append;
place.appendChild(ele);
return;
}
function eleget0(xpath) {
var ele = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
return ele.snapshotLength > 0 ? ele.snapshotItem(0) : "";
}
function elegeta(xpath) {
var ele = document.evaluate(xpath, document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
var array = [];
for (var i = 0; i < ele.snapshotLength; i++) array[i] = ele.snapshotItem(i);
return array;
}
})();
}
})();