Use DOM for shortcode replacement.

This commit is contained in:
osapon 2020-01-01 19:21:35 +09:00
parent b7c82b4831
commit 7b2254948b

View File

@ -845,23 +845,9 @@ function parse(obj, mix, acct_id, tlid, popup, mutefilter, type) {
} }
//絵文字があれば //絵文字があれば
if (emojick) { if (emojick) {
Object.keys(toot.emojis).forEach(function(key5) { content = this.customEmojiReplace(content, toot, gif);
var emoji = toot.emojis[key5] spoil = this.customEmojiReplace(spoil, toot, gif);
var shortcode = emoji.shortcode poll = this.customEmojiReplace(poll, toot, gif);
if (gif == 'yes') {
var emoSource = emoji.url
} else {
var emoSource = emoji.static_url
}
var emoji_url = `
<img draggable="false" src="${emoSource}" class="emoji-img" data-emoji="${shortcode}"
alt=" :${shortcode}: " title="${shortcode}" onclick="this.classList.toggle('bigemoji');">
`
var regExp = new RegExp(':' + shortcode + ':', 'g')
content = content.replace(regExp, emoji_url)
spoil = spoil.replace(regExp, emoji_url)
poll = poll.replace(regExp, emoji_url)
})
} }
//ニコフレ絵文字 //ニコフレ絵文字
if (toot.profile_emojis) { if (toot.profile_emojis) {
@ -1513,3 +1499,48 @@ function pollParse(poll, acct_id, emojis) {
</div>` </div>`
return pollHtml 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;
}