Skip to content

Instantly share code, notes, and snippets.

@zklhp
Last active June 21, 2019 06:36
Show Gist options
  • Save zklhp/feddc94e4cf53d01091203413ec4e893 to your computer and use it in GitHub Desktop.
Save zklhp/feddc94e4cf53d01091203413ec4e893 to your computer and use it in GitHub Desktop.
xuexi.cn study automatically UserScript
// ==UserScript==
// @name xuexi.cn study automatically
// @namespace https://github.com/zklhp
// @icon https://www.xuexi.cn/favicon.ico
// @version 1.1.7
// @homepageURL https://gist.github.com/zklhp/feddc94e4cf53d01091203413ec4e893
// @updateURL https://gist.github.com/zklhp/feddc94e4cf53d01091203413ec4e893/raw/xuexicn-study-automatically.user.js
// @description With this, you can view articles and videos automatically on xuexi.cn
// @author Chris Zheng <https://chriszheng.science/>
// @grant unsafeWindow
// @match *://*.xuexi.cn/*
// ==/UserScript==
// unsafeWindow.document.hasFocus = function() {return true;};
Object.defineProperties(document.wrappedJSObject,
{ 'hidden': {value: false}, 'visibilityState': {value: 'visible'} });
window.addEventListener('visibilitychange', evt => evt.stopImmediatePropagation(), true);
const sleep = ((ms = 0) => new Promise(resolve => setTimeout(resolve, ms)));
const random_click = (ele, i = 1) => {
// With i == 1, random click.
let l = document.querySelectorAll(ele);
if (l.length == 0) {
return false;
}
else {
l[(i == 0) ? 0 : Math.floor(Math.random() * l.length)].click();
return true;
}
}
(async () => {
while (1) {
console.log(new Date().toString());
unsafeWindow.focus(); // May be useless.
await sleep(60 * 1000); // Sleep 1 min.
random_click("span.list");
if (!random_click("div.text-wrap"))
{
// Cannot click.
console.log("This is a page, sleep and close.");
let countdown = (5 + Math.random() * 5) * 60; // Unit is second.
while (countdown-- >= 0) {
// Restart the video.
random_click("div.pause", 0);
console.log("Click to continue video.");
await sleep(1000); // Sleep 1 second.
// console.log("Countdown: " + countdown);
}
if (document.title == "我的积分") {
location.reload();
}
// We have slept 5 to 10 min, close the page.
window.close();
}
else
{
console.log("This is a catalog page, click to open new page and sleep.");
await sleep(15 * 60 * 1000); // Sleep 15 min. Less pages to open.
await sleep(60 * 60 * 1000); // Sleep 60 min.
// Now jump page or refresh the page.
if (!random_click("div.btn")) {
location.reload(); // Or we refresh.
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment