diff --git a/app/js/tl/parse.js b/app/js/tl/parse.js index 8bb4418b..63e3bc3f 100644 --- a/app/js/tl/parse.js +++ b/app/js/tl/parse.js @@ -821,23 +821,9 @@ function parse(obj, mix, acct_id, tlid, popup, mutefilter, type, onlyContent) { } //絵文字があれば if (emojick) { - Object.keys(toot.emojis).forEach(function (key5) { - var emoji = toot.emojis[key5] - var shortcode = emoji.shortcode - if (gif == 'yes') { - var emoSource = emoji.url - } else { - var emoSource = emoji.static_url - } - var emoji_url = ` -  :${shortcode}: - ` - var regExp = new RegExp(':' + shortcode + ':', 'g') - content = content.replace(regExp, emoji_url) - spoil = spoil.replace(regExp, emoji_url) - poll = poll.replace(regExp, emoji_url) - }) + content = this.customEmojiReplace(content, toot, gif); + spoil = this.customEmojiReplace(spoil, toot, gif); + poll = this.customEmojiReplace(poll, toot, gif); } //ニコフレ絵文字 if (toot.profile_emojis) { @@ -1529,7 +1515,51 @@ function pollParse(poll, acct_id, emojis) { ` return pollHtml } +function customEmojiReplace(content, toot, gif) { + var contentDiv = document.createElement('div'); + contentDiv.style.display = 'none'; + contentDiv.innerHTML = content; + var replace = false; + Object.keys(toot.emojis).forEach(function(key5) { + var emoji = toot.emojis[key5] + var shortcode = emoji.shortcode + if (gif == 'yes') { + var emoSource = emoji.url + } else { + var emoSource = emoji.static_url + } + var nodes = document.evaluate('//text()', contentDiv, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ); + if ( nodes.snapshotLength == 0 ) return; + var shortcodeBracket = (':' + shortcode + ':'); + var img = document.createElement('img'); + img.setAttribute('draggable', 'false'); + img.setAttribute('src', emoSource); + img.setAttribute('class', 'emoji-img'); + img.setAttribute('data-emoji', shortcode); + img.setAttribute('alt', shortcodeBracket); + img.setAttribute('title', shortcodeBracket); + img.setAttribute('onclick', "this.classList.toggle('bigemoji');"); + + for ( var i=0; i < nodes.snapshotLength; i++ ) { + var node = nodes.snapshotItem(i); + var txt = node.textContent; + var spos = txt.indexOf(shortcodeBracket); + if (spos == -1) continue; + + var txtBefore = document.createTextNode(txt.substr(0, spos)); + var txtAfter = document.createTextNode(txt.substr(spos + shortcodeBracket.length)); + + node.parentNode.insertBefore(txtBefore, node); + node.parentNode.insertBefore(img, node); + node.parentNode.insertBefore(txtAfter, node); + node.textContent = ''; + replace = true; + } + }) + if (replace) contentDiv.innerHTML = this.customEmojiReplace(contentDiv.innerHTML, toot, gif); + return contentDiv.innerHTML; +} //MastodonBaseStreaming var mastodonBaseWs = {} var mastodonBaseWsStatus = {}