Skip to content

Instantly share code, notes, and snippets.

@cyanzhong

cyanzhong/WeiboPIP.js

Last active Jan 8, 2021
Embed
What would you like to do?
PiP & Downloader for Weibo videos
const url = $clipboard.link || "";
if (url.length == 0) {
alert("Please copy the video link first");
return;
}
const webView = $ui.create({
views: [
{
type: "web",
props: { url },
events: {
didFinish: didFinishLoading
}
}
]
});
function didFinishLoading(sender) {
const timer = setInterval(async() => {
const script =
`
(() => {
const html = document.documentElement.innerHTML;
const regex = new RegExp("(https?:\/\/f\.video\.weibocdn\.com\/.+template=)(.+?)x.+?,video", "g");
const matches = html.matchAll(regex) || [];
if (matches.length == 0) {
return null;
}
let url = null;
let res = 0;
for (const match of matches) {
const _url = match[0];
const _res = parseFloat(match[2]);
if (_res > res) {
res = _res;
url = _url;
}
}
return url;
})();
`;
const url = (await sender.eval(script))[0];
if (url) {
timer.invalidate();
webView.remove();
const unescaped = $text.HTMLUnescape(url);
const {index} = await $ui.menu(["Download", "Picture in Picture"]);
if (index == 0) {
download(unescaped);
} else if (index == 1) {
pip(unescaped);
}
}
}, 200);
}
async function download(url) {
const {data} = await $http.download({
url, showsProgress: true
});
$share.sheet(data);
}
function pip(url) {
$app.openURL(url);
$app.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment