Skip to content

Instantly share code, notes, and snippets.

@JimmyLv
Last active September 26, 2022 04:51
Show Gist options
  • Save JimmyLv/896fee3a29357d8e9ef033cd036851c7 to your computer and use it in GitHub Desktop.
Save JimmyLv/896fee3a29357d8e9ef033cd036851c7 to your computer and use it in GitHub Desktop.
将时间戳转换为可点击的按钮 作者:吕立青_JimmyLv https://www.bilibili.com/read/cv18780064?spm_id_from=333.999.0.0 出处:bilibili
/*
------------------------------
trans timestamp in bilibili-note
thanks to @Note必利阀 [让b站视频笔记回归b站,本地obsidian太孤单了_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV17B4y1y7Ut)
------------------------------
main
- trans text timestamp into original time click button
- usage:
- open bilibili note editor
- set at least one time button in editor
- run this script
*/
var editor = document.querySelector(
"#app > div.resizable-component.bili-note.active-note > div.editor-innter.ql-container.ql-snow > div.ql-editor"
);
if (!editor) {
alert("📝笔记编辑器未打开!");
}
// get first timestamp node as template
// clone it
var timeStampFound = editor.querySelector("[data-cid]");
if (!timeStampFound) {
alert("🕐笔记中需要打上第一个时间戳作为模板!");
} else {
var tmpTimeStamp = timeStampFound.cloneNode(true);
}
// var timeRex = /\[(\d\d:.*\d\d)(\s·\s)?(.*?)\]\(https:\/\/www\.bilibili\.com\/video.*?[#\?]t=([^#?]*)\)$/;
var timeRex = /([0-9]{1,2}):([0-9]{1,2})(?::([0-9]{1,2}))?/;
if (editor && tmpTimeStamp) {
for (e of editor.children) {
var note = e.textContent;
var matched = note.match(timeRex);
if (matched) {
console.log("========matched========", matched);
const [timeText, time1, time2, time3] = matched;
const hour = time3 ? time1 : "00";
const minute = time3 ? time2 : time1;
const second = time3 ? time3 : time2;
var secs = Number(hour) * 60 * 60 + Number(minute) * 60 + Number(second);
// todo: maybe parse #tag as title
var title = note.replace(timeText, "");
console.log(timeText, secs, title);
// replace inner html
// data-seconds
var ts = tmpTimeStamp.cloneNode(true);
ts.setAttribute("data-seconds", secs);
ts.querySelector(".time-tag-item__text").firstChild.nodeValue =
timeText.startsWith("00:") ? timeText.replace("00:", "") : timeText;
/* if (ts.querySelector(".time-tag-item__desc")?.textContent) {
ts.querySelector(".time-tag-item__desc").textContent = title
? " · " + title
: "";
} */
e.textContent = title;
e.prepend(ts);
}
}
alert("✅时间戳转化完成:)");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment