Skip to content

Instantly share code, notes, and snippets.

@BusyJay
Last active October 3, 2016 10:57
Show Gist options
  • Save BusyJay/f2a20558621ea75c47c067f7b9348e8e to your computer and use it in GitHub Desktop.
Save BusyJay/f2a20558621ea75c47c067f7b9348e8e to your computer and use it in GitHub Desktop.
Collect small tv in bilibili automatically
// to use this script, you should allow popups on live.bilibili.com.
function collect(url, times, w, cb) {
var img = w.document.querySelector(".small-tv-draw").querySelector(".draw-img");
if (img.length == 0) {
console.error("page " + url + " not loaded, there should be a bug.");
w.close();
cb();
} else {
img.click();
console.info(url + " is collected");
setTimeout(function() {
w.close();
cb();
}, 500);
}
};
function collectLoop(tvUrls) {
if (tvUrls.length == 0) {
setTimeout(function() { collectLoop(tvUrls) }, 1000);
return;
}
var url = tvUrls.shift();
var w = window.open(url, "_blank");
$(w).load(function() {
collect(url, 1, w, function() {
collectLoop(tvUrls);
});
});
}
function mutationHandlerFactory() {
var tvUrls = [];
collectLoop(tvUrls);
return function(mutationRecords) {
mutationRecords.forEach(function(mutation) {
if (mutation.type == "childList") {
mutation.addedNodes.forEach(function(node) {
var link = $(node).find(".small-tv-msg a");
if (link.length) {
let url = link.attr("href");
console.info("collecting: ", url);
tvUrls[tvUrls.length] = url;
}
})
}
});
};
}
var elemt = document.getElementById("chat-msg-list");
var obsConfig = {
childList: true,
characterData: false,
attributes: false,
subtree: false
};
var observer = new MutationObserver(mutationHandlerFactory());
observer.observe(elemt, obsConfig);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment