Quantcast
Channel: 簡睿隨筆
Viewing all articles
Browse latest Browse all 897

Obsidian裡產生YouTube影片iframe的作法:使用Dataviewjs

$
0
0

因為使用了Obsidian Soundscapes在Obsidian裡透過YouTube聽音樂,但有些影片無法被嵌入筆記裡,為了簡化偵測可嵌入的測試,在網頁裡加入下列Dataviewjs來快速測試:

  1. 輸入影片代碼
  2. 點擊Create Video
  3. 若出現影片封面表示可也都內嵌

gh|700

```dataviewjs
let eleHTML = dv.el("hidden", "");

dv.span("YouTube ID: ");
let eleID = dv.el("input");
const button = dv.el("button", "Create Video");

eleID.style.width = "200px";

eleID.onfocus = function() {
  eleID.value = "";
}

eleID.onblur = function() {
  let _sID = this.value;
  if (_sID.length != 11) {
    new Notice("YouTube ID wrong!");
    return;
  }
  let _sHTML = '<iframe id="IFRAME" width="650" height="315" src="https://www.youtube.com/embed/%ID%" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>';

  eleHTML.value = _sHTML;

  eleHTML.value = eleHTML.value.replace(/%ID%/, _sID);

  new Notice("onblur " + eleHTML.value);
}

button.onclick = function() {
  const ele = document.getElementById("IFRAME");
  if (ele) ele.remove(ele);

  dv.paragraph(eleHTML.value);
}


##

您可能也會有興趣的類似文章


Viewing all articles
Browse latest Browse all 897

Trending Articles