Last active
April 3, 2019 11:46
-
-
Save nekketsuuu/49f23f084ff0342a08233cf00588887e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
javascript:(function(){ | |
let comment = 'See https://gist.github.com/nekketsuuu/49f23f084ff0342a08233cf00588887e'; | |
const patIsYouTube = new RegExp(/youtube\.com/); | |
const patIsNiconico = new RegExp(/nicovideo\.jp/); | |
if (patIsYouTube.test(document.URL)) { | |
const apiKey = 'YOUR_API_KEY_HERE'; | |
const title = document.title.replace(/ - YouTube$/, '').trim(); | |
let link = ''; | |
let date = ''; | |
let name = ''; | |
const patIsLive = new RegExp(/\/live$/); | |
if (patIsLive.test(document.URL)) { | |
const channelId = document.URL.match(/^https?\:\/\/www\.youtube\.com\/channel\/([^\/]+)\/.*/)[1]; | |
if (!channelId) { | |
console.log('Error: channelId is empty'); | |
return; | |
} | |
const getUrl = 'https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=' + channelId + '&type=video&eventType=live&key=' + apiKey; | |
const xmlHttp = new XMLHttpRequest; | |
xmlHttp.open('GET', getUrl, false); | |
xmlHttp.send(null); | |
result = JSON.parse(xmlHttp.responseText); | |
if (!result.items[0].snippet.liveBroadcastContent) { | |
console.log('Error: liveBroadcastContent is false or null'); | |
return; | |
} | |
comment = 'tenuki'; | |
link = '[https://www.youtube.com/watch?v=' + result.items[0].id.videoId + ']'; | |
let rawDate = new Date(result.items[0].snippet.publishedAt); | |
let mn = rawDate.getMonth() + 1; | |
let ms = ''; | |
if (mn < 10) { | |
ms = '0' + mn; | |
} else { | |
ms = '' + mn; | |
} | |
let dn = rawDate.getDate(); | |
let ds = ''; | |
if (dn < 10) { | |
ds = '0' + dn; | |
} else { | |
ds = '' + dn; | |
} | |
date = '[' + rawDate.getFullYear() + '-' + ms + ']-' + ds; | |
name = result.items[0].snippet.channelTitle; | |
} else { | |
const patToTrimURL = new RegExp(/^https?\:\/\/www\.youtube\.com\/watch\?v=[^&#]+/); | |
link = '[' + patToTrimURL.exec(document.URL)[0] + ']'; | |
const preVideoId = document.URL.match(/watch\?v=([^&#]+).*/); | |
if (preVideoId) { | |
const videoId = preVideoId[1]; | |
const getUrl = 'https://www.googleapis.com/youtube/v3/videos?part=id%2C+snippet&id=' + videoId + '&key=' + apiKey; | |
const xmlHttp = new XMLHttpRequest; | |
xmlHttp.open('GET', getUrl, false); | |
xmlHttp.send(null); | |
result = JSON.parse(xmlHttp.responseText); | |
if (!result.items[0].snippet) { | |
console.log('Error: snippet is not found'); | |
return; | |
} | |
comment = 'tenuki'; | |
let rawDate = new Date(result.items[0].snippet.publishedAt); | |
let mn = rawDate.getMonth() + 1; | |
let ms = ''; | |
if (mn < 10) { | |
ms = '0' + mn; | |
} else { | |
ms = '' + mn; | |
} | |
let dn = rawDate.getDate(); | |
let ds = ''; | |
if (dn < 10) { | |
ds = '0' + dn; | |
} else { | |
ds = '' + dn; | |
} | |
date = '[' + rawDate.getFullYear() + '-' + ms + ']-' + ds; | |
name = result.items[0].snippet.channelTitle; | |
} | |
} | |
if (link !== '') { link = link + '\n\n'; } | |
if (date !== '') { date = date + '\n'; } | |
if (name !== '') { name = '#' + name + '\n'; } | |
const openUrl = 'https://scrapbox.io/nekketsuuu-private/' + encodeURIComponent(title) + '?body=' + encodeURIComponent(link + date + name + '\n'); | |
window.open(openUrl); | |
} else if (patIsNiconico.test(document.URL)) { | |
let title = document.title.replace(/- ニコニコ動画/, '').replace(/- ニコニコ生放送/, '').trim(); | |
let link = document.URL.replace(/#.*$/).replace(/\?.*$/).trim(); | |
let today = new Date(); | |
let mn = today.getMonth() + 1; | |
let ms = ''; | |
if (mn < 10) { | |
ms = '0' + mn; | |
} else { | |
ms = '' + mn; | |
} | |
let dn = today.getDate(); | |
let ds = ''; | |
if (dn < 10) { | |
ds = '0' + dn; | |
} else { | |
ds = '' + dn; | |
} | |
let date = '[' + today.getFullYear() + '-' + ms + ']-' + ds; | |
if (link !== '') { link = link + '\n\n'; } | |
if (date !== '') { date = date + '\n'; } | |
const openUrl = 'https://scrapbox.io/nekketsuuu-private/' + encodeURIComponent(title) + '?body=' + encodeURIComponent(link + date + '\n'); | |
window.open(openUrl); | |
} else { | |
console.log('Cannot recognize this website: ' + document.URL); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment