This repository has been archived on 2023-10-01. You can view files and clone it, but cannot push or open issues or pull requests.
PyNyaaTa/delay.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-11-25 21:52:22 +00:00
// https://stackoverflow.com/a/41017165
"use strict";
var page = require('webpage').create(),
system = require('system'),
mustQuit = false,
canShow = false,
underAttack = false,
address, delay;
if (system.args.length < 3 || system.args.length > 5) {
console.log('Usage: delay.js URL delay');
phantom.exit(1);
} else {
address = system.args[1];
delay = system.args[2];
page.open(address, function (status) {
if (status !== 'success') {
phantom.exit(1);
} else {
window.setTimeout(function () {
if (underAttack && canShow) {
console.log(page.content);
phantom.exit();
} else {
phantom.exit(503);
}
}, delay);
window.setTimeout(function () {
if (mustQuit) {
phantom.exit(429);
} else if (!underAttack && canShow) {
console.log(page.content);
phantom.exit();
}
}, 1);
}
});
page.onResourceReceived = function (response) {
switch (response.status) {
case 200:
canShow = true;
break;
case 429:
mustQuit = true;
break;
case 503:
underAttack = true;
break;
}
};
}