thedesk/app/js/tl/card.js

439 lines
12 KiB
JavaScript
Raw Normal View History

2018-01-28 23:22:43 +11:00
//カード処理やメンション、ハッシュタグの別途表示
//全てのTL処理で呼び出し
function additional(acct_id, tlid) {
//メンション系
2018-05-12 04:12:25 +10:00
//$(".mention").attr("href", "");
2019-05-19 17:39:30 +10:00
2019-11-09 00:52:54 +11:00
$('#timeline-container .mention').addClass('parsed')
2018-05-12 04:12:25 +10:00
2019-11-09 00:52:54 +11:00
$('#timeline-container .hashtag, #timeline-container [rel=tag]').each(function(i, elem) {
2019-11-04 03:10:06 +11:00
var tags = $(this)
2019-11-09 00:52:54 +11:00
.attr('href')
.match(/https?:\/\/([-a-zA-Z0-9@.]+)\/tags\/([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/)
2019-11-04 03:10:06 +11:00
if (tags) {
2019-11-09 00:52:54 +11:00
var tagThis = tags[2]
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
var tagThis = $(this).attr('data-tag')
2019-09-17 01:22:55 +10:00
}
2019-11-04 03:10:06 +11:00
if (tagThis) {
2019-11-09 00:52:54 +11:00
$(this).attr('onclick', "tagShow('" + tagThis + "')")
$(this).attr('href', '#')
2018-05-12 04:12:25 +10:00
}
2019-11-09 00:52:54 +11:00
})
2019-05-19 17:39:30 +10:00
2018-01-28 23:22:43 +11:00
//トゥートサムネ
2019-11-09 00:52:54 +11:00
$('#timeline_' + tlid + ' .toot a:not(.parsed)').each(function(i, elem) {
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var card = localStorage.getItem('card_' + tlid)
var text = $(this).attr('href')
2019-05-19 17:39:30 +10:00
if (text) {
2019-11-09 00:52:54 +11:00
if (text.indexOf('twimg.com') === -1) {
var urls = text.match(
/https?:\/\/([-a-zA-Z0-9@.]+)\/media\/([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/
)
2018-08-21 04:26:14 +10:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
text = ''
var urls = []
2018-08-17 03:21:40 +10:00
}
2019-05-19 17:39:30 +10:00
2018-04-09 00:17:33 +10:00
//トゥートのURLぽかったら
2019-11-09 00:52:54 +11:00
toot = text.match(/https:\/\/([a-zA-Z0-9.-]+)\/@([a-zA-Z0-9_]+)\/([0-9]+)/)
2019-05-19 17:39:30 +10:00
if (toot) {
if (toot[1]) {
2019-11-09 00:52:54 +11:00
$(this).attr('data-acct', acct_id)
2018-04-09 00:17:33 +10:00
}
}
2018-01-28 23:22:43 +11:00
if (urls) {
2019-11-09 00:52:54 +11:00
$(this).remove()
2018-01-28 23:22:43 +11:00
} else if (!card) {
2019-11-04 03:10:06 +11:00
var id = $(this)
2019-11-09 00:52:54 +11:00
.parents('.cvo')
.attr('toot-id')
if (localStorage.getItem('mode_' + domain) == 'misskey') {
var start = 'https://' + domain + '/url?url=' + text
2018-08-17 03:21:40 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2018-08-17 03:21:40 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json'
2018-08-17 03:21:40 +10:00
}
2019-11-04 03:10:06 +11:00
//body: JSON.stringify({})
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json.title) {
2019-11-09 00:52:54 +11:00
$('[toot-id=' + id + '] .additional').html(
'<span class="gray">URL' +
lang.lang_cards_check +
':<br>Title:' +
escapeHTML(json.title) +
'<br>' +
escapeHTML(json.description) +
'</span>'
)
$('[toot-id=' + id + '] a:not(.parsed)').addClass('parsed')
$('[toot-id=' + id + ']').addClass('parsed')
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2018-08-17 03:21:40 +10:00
}
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
$(this).attr('title', text)
2018-01-28 23:22:43 +11:00
}
2019-11-09 00:52:54 +11:00
})
$('i.unparsed').each(function(i, elem) {
var dem = $(this).text()
var dom = $(this)
var start = './js/emoji/emoji-map.json'
var xmlHttpRequest = new XMLHttpRequest()
2019-11-04 03:10:06 +11:00
xmlHttpRequest.onreadystatechange = function() {
2019-05-19 17:39:30 +10:00
if (this.readyState === 4 && this.status === 200) {
if (this.response) {
2019-11-09 00:52:54 +11:00
var json = this.response
2019-11-04 03:10:06 +11:00
if (this.status !== 200) {
2019-11-09 00:52:54 +11:00
setLog(start, this.status, this.response)
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
var emojis = json.emojis
2019-05-19 17:39:30 +10:00
for (i = 0; i < emojis.length; i++) {
2019-11-09 00:52:54 +11:00
var emojie = emojis[i]
var regExp = new RegExp(dem, 'g')
2019-05-19 17:39:30 +10:00
if (emojie.emoji.match(regExp)) {
2019-11-09 00:52:54 +11:00
var sc = emojie.name
var sc = 'twa-' + sc.replace(/_/g, '-')
dom.addClass(sc)
dom.text('')
dom.removeClass('unparsed')
break
2019-05-19 17:39:30 +10:00
}
}
}
}
2019-11-09 00:52:54 +11:00
}
xmlHttpRequest.open('GET', start, true)
xmlHttpRequest.responseType = 'json'
xmlHttpRequest.send(null)
})
2019-05-19 17:39:30 +10:00
2019-11-09 00:52:54 +11:00
$('#timeline_' + tlid + ' .toot:not(:has(a:not(.add-show,.parsed)))').each(function(i, elem) {
2019-11-04 03:10:06 +11:00
$(this)
.parent()
2019-11-09 00:52:54 +11:00
.find('.add-show')
.hide()
})
2018-01-31 03:43:01 +11:00
//Markdownイメージビューワー
2019-11-09 00:52:54 +11:00
$('#timeline_' + tlid + ' .toot a:not(.img-parsed):has(img)').each(function(i, elem) {
var ilink = $(this).attr('href')
2019-11-04 03:10:06 +11:00
var id = $(this)
2019-11-09 00:52:54 +11:00
.parents('.cvo')
.attr('toot-id')
$(this).attr('href', '#')
$(this).attr('onclick', "imgv('" + id + "','" + i + "')")
$(this).attr('data-type', 'image')
$(this).attr('id', id + '-image-' + i)
$(this).attr('data-url', ilink)
$(this).addClass('img-parsed')
})
2018-01-28 23:22:43 +11:00
}
2018-02-05 01:56:31 +11:00
function additionalIndv(tlid, acct_id, id) {
2019-11-09 00:52:54 +11:00
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var text = $('[toot-id=' + id + '] .toot a').attr('href')
var urls = text.match(
/https?:\/\/([-a-zA-Z0-9@.]+)\/media\/([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/
)
2019-05-19 17:39:30 +10:00
if (urls) {
2019-11-09 00:52:54 +11:00
$('[toot-id=' + id + '] .toot a').remove()
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
if (localStorage.getItem('mode_' + domain) == 'misskey') {
var start = 'https://' + domain + '/url?url=' + text
2019-05-19 17:39:30 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-05-19 17:39:30 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json'
2019-05-19 17:39:30 +10:00
}
2019-11-04 03:10:06 +11:00
//body: JSON.stringify({})
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
if (json.title) {
2019-11-09 00:52:54 +11:00
$('[toot-id=' + id + '] .additional').html(
`<span class="gray">URL${lang.lang_cards_check}:<br>Title:${escapeHTML(
json.title
)}<br>${escapeHTML(json.description)}</span>`
)
$('[toot-id=' + id + '] a:not(.parsed)').addClass('parsed')
$('[toot-id=' + id + ']').addClass('parsed')
2019-11-04 03:10:06 +11:00
}
2019-11-09 00:52:54 +11:00
})
2018-02-05 01:56:31 +11:00
} else {
2019-11-09 00:52:54 +11:00
var id = $('[toot-id=' + id + '] .toot a')
.parents('.cvo')
.attr('toot-id')
var start = 'https://' + domain + '/api/v1/statuses/' + id
2019-05-19 17:39:30 +10:00
fetch(start, {
2019-11-09 00:52:54 +11:00
method: 'GET',
2019-05-19 17:39:30 +10:00
headers: {
2019-11-09 00:52:54 +11:00
'content-type': 'application/json',
Authorization: 'Bearer ' + at
2019-11-04 03:10:06 +11:00
}
2019-05-19 17:39:30 +10:00
//body: JSON.stringify({})
2019-11-04 03:10:06 +11:00
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
2019-11-09 00:52:54 +11:00
setLog(response.url, response.status, text)
})
2019-05-19 17:39:30 +10:00
}
2019-11-09 00:52:54 +11:00
return response.json()
2019-11-04 03:10:06 +11:00
})
.catch(function(error) {
2019-11-09 00:52:54 +11:00
todo(error)
setLog(start, 'JSON', error)
console.error(error)
2019-11-04 03:10:06 +11:00
})
.then(function(json) {
2019-11-14 04:42:06 +11:00
cards = json.card
var analyze = cardHtml(cards, acct_id, id)
$('[toot-id=' + id + '] .additional').html(analyze)
2018-07-22 23:03:46 +10:00
if (json.title) {
2019-11-09 00:52:54 +11:00
$('[toot-id=' + id + '] a:not(.parsed)').addClass('parsed')
$('[toot-id=' + id + ']').addClass('parsed')
2018-07-22 23:03:46 +10:00
}
2019-11-09 00:52:54 +11:00
})
2018-02-05 01:56:31 +11:00
}
2019-05-19 17:39:30 +10:00
}
2018-02-05 01:56:31 +11:00
}
2019-11-14 04:42:06 +11:00
function cardHtml(json, acct_id, id) {
var analyze = ''
var domain = json.url.match(/^https?:\/{2,}(.*?)(?:\/|\?|#|$)/)[1];
var ok = [
"pixiv.net",
"twitter.com",
"mobile.twitter.com",
"open.spotify.com",
"youtube.com",
"youtu.be",
"m.youtube.com",
2019-11-16 16:29:06 +11:00
"www.youtube.com",
2019-11-14 04:48:49 +11:00
"nicovideo.jp",
"twitcasting.tv"
2019-11-14 04:42:06 +11:00
]
var isHad = _.includes(ok, domain);
2019-11-14 04:48:49 +11:00
if (json.provider_name == 'pixiv') {
2019-11-14 04:42:06 +11:00
if (json.image) {
var pxvImg = `
<br><img src="${json.image}" style="max-width:100%"
onclick="imgv('pixiv_${id}',0,'pixiv')" id="pixiv_${id}-image-0"
data-url="${json.embed_url}"
data-type="image">
`
} else {
var pxvImg = ''
}
analyze = `<div class="pixiv-post"><b><a href="
${json.author_url}
" target="_blank">
${escapeHTML(json.author_name)}
</a></b><br>
${escapeHTML(json.title)}
${pxvImg}
</div>`
} else {
if (json.title) {
analyze = `<span class="gray">URL
${lang.lang_cards_check}
:<br>Title:
${escapeHTML(json.title)}
<br>
${escapeHTML(json.description)}
</span>`
}
2019-11-14 04:48:49 +11:00
if (json.html || json.provider_name == 'Twitter') {
2019-11-14 04:42:06 +11:00
if(isHad) {
var prved = `<img class="emoji" draggable="false" alt="✅"
src="https://twemoji.maxcdn.com/v/12.1.3/72x72/2705.png">`
2019-11-18 03:07:59 +11:00
var title = lang.lang_cards_trusted
2019-11-14 04:42:06 +11:00
}else{
2019-11-16 05:42:23 +11:00
var prved = '<img class="emoji" draggable="false" alt="⚠️" src="https://twemoji.maxcdn.com/v/12.1.4/72x72/26a0.png">'
2019-11-18 03:07:59 +11:00
var title = lang.lang_cards_untrusted
2019-11-14 04:42:06 +11:00
}
2019-11-14 04:48:49 +11:00
analyze =`<a onclick="cardHtmlShow('${acct_id}','${id}')" class="add-show pointer" title="${title}">
2019-11-14 04:42:06 +11:00
${lang.lang_parse_html}(${domain})${prved}
2019-11-14 04:48:49 +11:00
</a>${analyze}<br>`
2019-11-14 04:42:06 +11:00
}
}
return analyze
}
function cardHtmlShow(acct_id, id) {
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var text = $('[toot-id=' + id + '] .toot a').attr('href')
var urls = text.match(
/https?:\/\/([-a-zA-Z0-9@.]+)\/media\/([-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)/
)
var id = $('[toot-id=' + id + '] .toot a')
.parents('.cvo')
.attr('toot-id')
var start = 'https://' + domain + '/api/v1/statuses/' + id
fetch(start, {
method: 'GET',
headers: {
'content-type': 'application/json',
Authorization: 'Bearer ' + at
}
//body: JSON.stringify({})
})
.then(function(response) {
if (!response.ok) {
response.text().then(function(text) {
setLog(response.url, response.status, text)
})
}
return response.json()
})
.catch(function(error) {
todo(error)
setLog(start, 'JSON', error)
console.error(error)
})
.then(function(json) {
json = json.card
if (json.html) {
analyze =
json.html +
`<i class="material-icons sml pointer" onclick="pip('
${id}
')" title="
${lang.lang_cards_pip}
">picture_in_picture_alt</i>`
}
2019-11-14 04:48:49 +11:00
if (json.provider_name == 'Twitter') {
var url = json.author_url
var status = json.url.match(/^https:\/\/twitter.com\/[_a-zA-Z0-9-]+\/status\/([0-9]+)/);
2019-11-17 00:55:49 +11:00
var statusId = false
2019-11-14 04:48:49 +11:00
if(status){
if(status.length > 0){
2019-11-17 00:55:49 +11:00
var statusId = status[1]
2019-11-14 04:48:49 +11:00
}
}
2019-11-17 00:55:49 +11:00
console.log(statusId)
if(statusId){
url = json.url
analyze = `
<blockquote class="twitter-tweet" data-dnt="true"><strong>${json.author_name}</strong><br>${json.description}<a href="${url}">${json.url}</a></blockquote>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
`
}
2019-11-14 04:48:49 +11:00
}
2019-11-14 04:42:06 +11:00
$('[toot-id=' + id + '] .additional').html(analyze)
})
}
2018-01-28 23:22:43 +11:00
//各TL上方のLink[On/Off]
function cardToggle(tlid) {
2019-11-09 00:52:54 +11:00
var card = localStorage.getItem('card_' + tlid)
2018-01-28 23:22:43 +11:00
if (!card) {
2019-11-09 00:52:54 +11:00
localStorage.setItem('card_' + tlid, 'true')
$('#sta-card-' + tlid).text('Off')
$('#sta-card-' + tlid).css('color', 'red')
2018-01-28 23:22:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
localStorage.removeItem('card_' + tlid)
$('#sta-card-' + tlid).text('On')
$('#sta-card-' + tlid).css('color', '#009688')
2018-01-28 23:22:43 +11:00
}
}
//各TL上方のLink[On/Off]をチェック
function cardCheck(tlid) {
2019-11-09 00:52:54 +11:00
var card = localStorage.getItem('card_' + tlid)
2018-01-28 23:22:43 +11:00
if (!card) {
2019-11-09 00:52:54 +11:00
$('#sta-card-' + tlid).text('On')
$('#sta-card-' + tlid).css('color', '#009688')
2018-01-28 23:22:43 +11:00
} else {
2019-11-09 00:52:54 +11:00
$('#sta-card-' + tlid).text('Off')
$('#sta-card-' + tlid).css('color', 'red')
2018-01-28 23:22:43 +11:00
}
}
2018-05-02 14:14:03 +10:00
2019-05-19 17:39:30 +10:00
function mov(id, tlid, type) {
2019-11-09 00:52:54 +11:00
var click = false
if (tlid == 'notf') {
var tlide = '[data-notf=' + acct_id + ']'
} else if (tlid == 'user') {
var tlide = '#his-data'
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
var tlide = '[tlid=' + tlid + ']'
2018-05-02 14:14:03 +10:00
}
2019-11-09 00:52:54 +11:00
var mouseover = localStorage.getItem('mouseover')
2019-05-19 17:39:30 +10:00
if (!mouseover) {
2019-11-09 00:52:54 +11:00
mouseover = ''
2018-08-17 03:21:40 +10:00
}
2019-11-09 00:52:54 +11:00
if (mouseover == 'yes') {
mouseover = 'hide'
} else if (mouseover == 'click') {
if (type == 'mv') {
mouseover = ''
2019-05-19 17:39:30 +10:00
} else {
2019-11-09 00:52:54 +11:00
mouseover = 'hide'
2018-08-17 03:21:40 +10:00
}
2019-11-09 00:52:54 +11:00
click = true
} else if (mouseover == 'no') {
mouseover = ''
2018-05-02 14:14:03 +10:00
}
2019-11-09 00:52:54 +11:00
if (mouseover == 'hide') {
2019-11-04 03:10:06 +11:00
if (click) {
2019-11-09 00:52:54 +11:00
$(tlide + ' [toot-id=' + id + ']').toggleClass('hide-actions')
2019-11-04 03:10:06 +11:00
} else {
2019-11-09 00:52:54 +11:00
$(tlide + ' [toot-id=' + id + ']').removeClass('hide-actions')
2019-10-20 15:27:19 +11:00
}
2019-11-04 03:10:06 +11:00
2019-10-20 15:27:19 +11:00
//$(tlide + " [toot-id=" + id + "] .area-vis").toggleClass("hide")
//$(tlide + " [toot-id=" + id + "] .area-actions").toggleClass("hide")
//$(tlide + " [toot-id=" + id + "] .area-side").toggleClass("hide")
2018-05-02 14:14:03 +10:00
}
}
2019-05-19 17:39:30 +10:00
function resetmv(type) {
2019-11-09 00:52:54 +11:00
var mouseover = localStorage.getItem('mouseover')
2019-05-19 17:39:30 +10:00
if (!mouseover) {
2019-11-09 00:52:54 +11:00
mouseover = ''
} else if (mouseover == 'yes') {
mouseover = 'hide'
} else if (mouseover == 'no') {
mouseover = ''
} else if (mouseover == 'click' && type != 'mv') {
mouseover = 'hide'
2018-05-02 14:14:03 +10:00
}
2019-11-09 00:52:54 +11:00
if (mouseover == 'hide') {
$('.cvo').addClass('hide-actions')
2019-10-20 15:27:19 +11:00
//$(".area-vis").addClass("hide");
//$(".area-actions").addClass("hide");
//$(".area-side").addClass("hide");
2018-05-02 14:14:03 +10:00
}
2019-11-04 03:10:06 +11:00
}