-
-
Save karasugawasu/73a7fc728c837c6f8a4e52308d0f819a to your computer and use it in GitHub Desktop.
LemmyでPeerTubeの埋め込みを有効(FediMovie限定)
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
<script> | |
let body = document.querySelector('body'); | |
const config = { | |
childList: true, | |
subtree: true | |
}; | |
const observer = new MutationObserver(records => { | |
observer.disconnect(); | |
checkTitle(); | |
checkTitle_Twitter(); | |
}) | |
window.addEventListener('load', () => { | |
checkTitle(); | |
checkTitle_Twitter(); | |
let body = document.querySelector('body'); | |
observer.observe(body, config); | |
}) | |
function checkTitle(){ | |
let count = 0; | |
let timer = setInterval(() => { | |
if(document.querySelector('div.post-title a.text-body') != null && document.querySelector('iframe.peertube_embed') === null){ | |
let url = document.querySelector('div.post-title a.text-body').getAttribute('href'); | |
let title = document.querySelector('div.post-title a.text-body').innerText; | |
if(url.startsWith('https://fedimovie.com/videos/watch/')){ | |
count = 11 | |
setEmbed(url, title); | |
} | |
} | |
count++; | |
if(count > 10){ | |
clearInterval(timer); | |
let body = document.querySelector('body'); | |
observer.observe(body, config); | |
} | |
}, 500) | |
} | |
function setEmbed(url, title){ | |
let embedUrl = url.replace('watch', 'embed'); | |
let embedHtml = '<iframe class="peertube_embed" title="' + title + '" style="border: 0; top: 0; left: 0; width: 100%; height: 100%; position: absolute;" src="' + embedUrl + '" frameborder="0" allowfullscreen="" sandbox="allow-same-origin allow-scripts allow-popups"></iframe>'; | |
let embedDiv = document.createElement('div'); | |
embedDiv.innerHTML = embedHtml; | |
embedDiv.style.cssText = 'overflow: hidden; height: 0; position: relative; padding-bottom: 56.5%;' | |
document.querySelector('div.card').before(embedDiv); | |
} | |
function checkTitle_Twitter(){ | |
let count = 0; | |
let timer = setInterval(() => { | |
if(document.querySelector('div.post-title a.text-body') != null && document.querySelector('div.twitter-tweet') === null){ | |
let url = document.querySelector('div.post-title a.text-body').getAttribute('href'); | |
let title = document.querySelector('div.post-title a.text-body').innerText; | |
let pattern = /^https:\/\/twitter.com\/.*\/status\/\d+/g; | |
if(url.match(pattern)){ | |
count = 11 | |
setEmbed_Twitter(url, title); | |
} | |
} | |
count++; | |
if(count > 10){ | |
clearInterval(timer); | |
let body = document.querySelector('body'); | |
observer.observe(body, config); | |
} | |
}, 500) | |
} | |
function setEmbed_Twitter(url, title){ | |
let embedUrl = url.replace('watch', 'embed'); | |
let embedHtml = '<blockquote class="twitter-tweet">'+ title +'<a href="' + url + '"></a></blockquote>'; | |
let embedDiv = document.createElement('div'); | |
embedDiv.innerHTML = embedHtml; | |
embedDiv.style.cssText = 'overflow: hidden; height: 0; position: relative; padding-bottom: 56.5%;' | |
document.querySelector('div.post-listing div.md-div').innerHTML = embedHtml; | |
(async ()=>{ | |
await loadScript("https://platform.twitter.com/widgets.js"); | |
})(); | |
} | |
function loadScript(src) { | |
return new Promise((resolve, reject) => { | |
const script = document.createElement("script"); | |
script.src = src; | |
script.onload = resolve; | |
document.head.appendChild(script); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment