首页 > 其他分享 >在博客园的博客文章中自动播放 asciinema

在博客园的博客文章中自动播放 asciinema

时间:2022-09-01 16:23:27浏览次数:55  
标签:style container script 博客园 asciinema iframe 自动播放 var

需求

我的文章中都是用 Markdown 写的,所以嵌入的 asciinema 只能通过 Embed image link 的方式,这就导致打开文章页面后只有一个 asciinema 的预览图,点击图片后会跳转到 asciinema 网页。我希望不需要跳转,而是直接可以在文章页面播放 asciinema。也就是希望可以 Embed the player,问题是文章页面中不能直接插入 js 代码

解决方案

  • 首先需要申请博客园的 js 权限
  • 然后在「页脚 HTML 代码」中插入以下代码:

<script type="text/javascript">
// asciinema embedded player
(function() {
  function insertAfter(referenceNode, newNode) {
    referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
  }

  function params(container, script) {
    function format(name) {
      var value = script.getAttribute('data-' + name);
      if (value) {
        return name + '=' + value;
      }
    }

    var options = ['size', 'speed', 'autoplay', 'loop', 'theme', 't', 'startAt', 'preload', 'cols', 'rows', 'i', 'idleTimeLimit'];

    return '?' + options.map(format).filter(Boolean).join('&');
  }

  function locationFromString(string) {
    var parser = document.createElement('a');
    parser.href = string;
    return parser;
  }

  function apiHostFromScript(script) {
    return "https://asciinema.org"
  }

  function insertPlayer(script) {
    // do not insert player if there's one already associated with this script
    if (script.dataset.player) {
      return;
    }

    var apiHost = apiHostFromScript(script);

    // var asciicastId = script.id.split('-')[1];
    var asciicastId = script.href.substring('https://asciinema.org/a/'.length);

    var container = document.createElement('div');
    container.id = "asciicast-container-" + asciicastId;
    container.className = 'asciicast';
    container.style.display = 'block';
    container.style.float = 'none';
    container.style.overflow = 'hidden';
    container.style.padding = 0;
    container.style.margin = '20px 0';

    insertAfter(script, container);
    // script.parentNode.removeChild(script);
    script.hidden = true;

    var iframe = document.createElement('iframe');
    iframe.src = apiHost + "/a/" + asciicastId + '/iframe' + params(container, script);
    iframe.id = "asciicast-iframe-" + asciicastId;
    iframe.name = "asciicast-iframe-" + asciicastId;
    iframe.scrolling = "no";
    iframe.setAttribute('allowFullScreen', 'true');
    iframe.style.overflow = "hidden";
    iframe.style.margin = 0;
    iframe.style.border = 0;
    iframe.style.display = "inline-block";
    iframe.style.width = "100%";
    iframe.style.float = "none";
    iframe.style.visibility = "hidden";
    iframe.onload = function() { this.style.visibility = 'visible' };

    container.appendChild(iframe);

    function receiveSize(e) {
      var name = e.data[0];
      var data = e.data[1];

      if (e.origin === apiHost && e.source === iframe.contentWindow && name === 'resize') {
        iframe.style.height = '' + data.height + 'px';
      }
    }

    window.addEventListener("message", receiveSize, false);

    script.dataset.player = container;
  }

  window.onload = e=>{
    // var scripts = document.querySelectorAll("script[id^='asciicast-']");
    var scripts = document.querySelectorAll("a[href^='https://asciinema.org/a/']");
  [].forEach.call(scripts, insertPlayer);
  }
  
})();
</script>

原理

页面加载完成后,自动插入 asciinema player,并隐藏原来的图片


EOF

标签:style,container,script,博客园,asciinema,iframe,自动播放,var
From: https://www.cnblogs.com/hangj/p/16646913.html

相关文章

  • 博客园主题第二弹
    页面定制css点击查看代码#cnblog-nava:nth-child(1)span:before{display:none;color:#f27474!important;}#cnblog-nava:nth-child(2)span:before{......
  • 8/30 注册博客园
    8月30日下午17:05鄙人写下了第一篇博客。写博客的目的很单纯,想通过记录博客的方式以记录自己的ACM学习,大学生活什么的。另外大佬们总会说要自己写解题报告,算法分析,模板什......
  • 将博客园内博客移至CSDN
    访问csdn搬家链接:https://mp.csdn.net/mp_blog/tools/move选择博客园,输入博客园地址、输入搬家通知地址......
  • 解决微信浏览器video视频自动播放的问题
    <videocontrols="controls"src=""id="video"x5-video-player-type="h5"preload="metadata"playsinline="true"webkit-playsinline="true"x-webkit-airplay="......
  • 对博客园图片进行统一缩放
    importosimportrefile=r'k8s.md'new_file='new_'+filepattern=re.compile(r"(\s*)!\[\]\((https://.*png)\)")withopen(file,'r',encoding='utf-8')as......
  • 我的博客园使用开始
    N年前就想过写博客。但是现在才开始写第一篇随笔。HelloWorld!博客园的各位使用者大家好,以前看到过很多优秀的文章和博文,对自己有很大的帮助。在后面的工作学习中要常......
  • 更改博客园的博客地址成功
    博客园的博客地址是可以更改的,通过发邮件给官方即可修改。本博客原址址是:https://www.cnblogs.com/xxxxxxxxxxxxxxxxxxxxx/  (隐藏)(更改会造成以前的博文链接失效,请不......
  • [转]WIFI智能配网 - SmartConfig - Ray Liang - 博客园
    要开始IoT项目的第一步是什么?当然不是硬件,而是硬件与硬件的连接!即使有各种各样的通信协议没有好的连接方式绝对不行。那外设上没有的屏幕,没有键盘怎末输入密码怎末选择网络......
  • 2022年入驻博客园
    过去三年,充满奇幻色彩的三年,注定会被历史铭记的三年,必将改变未来的三年。闲来无事,开通博客,随手记录,文笔拙劣,请见谅......
  • 博客园设置目录
    1.让博客园博客自动生成章节目录索引 https://www.cnblogs.com/wangqiguo/p/4355032.html#_labelTop效果:使用方法:登录到博客园之后,打开博客园的后台管理,切换到“设置......