81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
// 在页面里注入一个悬浮"下载视频"按钮,贴在当前视频播放器右下角。
|
|
// 点击后通过 postMessage 通知宿主程序开始下载流程。
|
|
(function () {
|
|
var BTN_ID = "qz-video-download-btn";
|
|
|
|
function post(msg) {
|
|
try {
|
|
if (
|
|
window.chrome &&
|
|
window.chrome.webview &&
|
|
window.chrome.webview.postMessage
|
|
) {
|
|
window.chrome.webview.postMessage(JSON.stringify(msg));
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
function bigVideo() {
|
|
var vids = document.querySelectorAll("video");
|
|
var best = null,
|
|
bestArea = 0;
|
|
for (var i = 0; i < vids.length; i++) {
|
|
var r = vids[i].getBoundingClientRect();
|
|
var area = r.width * r.height;
|
|
if (r.width > 120 && r.height > 60 && area > bestArea) {
|
|
best = vids[i];
|
|
bestArea = area;
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
function ensureButton() {
|
|
var btn = document.getElementById(BTN_ID);
|
|
var v = bigVideo();
|
|
|
|
if (!btn && v) {
|
|
btn = document.createElement("button");
|
|
btn.id = BTN_ID;
|
|
btn.type = "button";
|
|
btn.innerText = "下载视频";
|
|
btn.style.cssText =
|
|
"position:fixed;z-index:2147483647;padding:5px 12px;border:0;border-radius:16px;" +
|
|
"background:#ff2d55;color:#fff;font-size:13px;line-height:1.4;cursor:pointer;" +
|
|
'box-shadow:0 2px 8px rgba(0,0,0,.35);font-family:system-ui,"Microsoft YaHei",sans-serif;';
|
|
btn.addEventListener(
|
|
"click",
|
|
function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
post({ type: "downloadClick", pageUrl: location.href });
|
|
},
|
|
true,
|
|
);
|
|
document.documentElement.appendChild(btn);
|
|
}
|
|
|
|
if (btn) {
|
|
if (!v) {
|
|
btn.style.display = "none";
|
|
return;
|
|
}
|
|
var r = v.getBoundingClientRect();
|
|
btn.style.display = "block";
|
|
btn.style.left = Math.max(8, r.right - btn.offsetWidth - 12) + "px";
|
|
btn.style.top = Math.max(8, r.bottom - btn.offsetHeight - 12) + "px";
|
|
}
|
|
}
|
|
|
|
// 播放进度变化时(抖音翻页、切换清晰度)重新定位
|
|
document.addEventListener("timeupdate", ensureButton, true);
|
|
window.addEventListener("resize", ensureButton, true);
|
|
window.addEventListener("scroll", ensureButton, true);
|
|
document.addEventListener("mouseover", ensureButton, true);
|
|
|
|
if (!window.__downloadBtnTimer) {
|
|
window.__downloadBtnTimer = setInterval(ensureButton, 800);
|
|
}
|
|
ensureButton();
|
|
})();
|