Last active
February 26, 2018 15:05
-
-
Save anzfactory/91dae11960687b65ee99a8d597b03a17 to your computer and use it in GitHub Desktop.
tumblrにgist埋め込みをする https://anz-note.ghost.io/2018/02/26/2245/
This file contains hidden or 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> | |
/** | |
* gistをiframeで読み込むやつ | |
* なぜ必要なのか? | |
* tumblrで記事に埋め込んだgist埋め込みタグが表示されないからだ! | |
* | |
* <div class="gist-container" data-gist-id="a4751a81660fce424b1d6127c0ea4191"></div> | |
* 埋め込みたいところにこんな感じで埋め込む! | |
* そして下記スクリプトをテンプレートの方にでも仕込んでおく! | |
*/ | |
function loadGists() { | |
let gists = document.querySelectorAll(".gist-container") | |
gists.forEach(function(container) { | |
let frame = document.createElement("iframe") | |
frame.width = "100%" | |
frame.frameBorder = "0" | |
container.appendChild(frame) | |
let gistId = container.dataset.gistId | |
let doc = frame.document | |
if (frame.contentDocument) { | |
doc = frame.contentDocument | |
} | |
doc.open() | |
doc.writeln('<html><body style="overflow: hidden;" onload="parent.adjust(document.body.scrollHeight, \'' + gistId + '\')"><scr' + 'ipt src="https://gist.github.com/' + gistId + '.js"></sc'+'ript><base target="_parent" /></b' + 'ody></html>') | |
doc.close() | |
}) | |
} | |
function adjust(newHeight, gistId) { | |
let target = document.querySelector('.gist-container[data-gist-id="' + gistId + '"]>iframe') | |
if (target) { | |
target.height = parseInt(newHeight) | |
} | |
} | |
loadGists() | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment