Skip to content

Commit

Permalink
Prevent querying image limits in background tabs
Browse files Browse the repository at this point in the history
Chrome's background optimization may lets all background tabs request at the same time, which may makes your IP blocked if you opened too many tabs.
  • Loading branch information
ccloli committed Jun 11, 2021
1 parent 0a0647e commit 3802022
Showing 1 changed file with 54 additions and 34 deletions.
88 changes: 54 additions & 34 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var getAllPagesURLFin = false;
var pretitle = document.title;
var needTitleStatus = false;
var delayTime = 0;
var visibleState = true;
var fetchPagesXHR = new XMLHttpRequest();
var emptyAudio;
var emptyAudioFile = 'data:audio/mp3;base64,SUQzBAAAAAAAI1RTU0UAAAAPAAADTGF2ZjU3LjcxLjEwMAAAAAAAAAAAAAAA/+M4wAAAAAAAAAAAAEluZm8AAAAPAAAAEAAABVgANTU1NTU1Q0NDQ0NDUFBQUFBQXl5eXl5ea2tra2tra3l5eXl5eYaGhoaGhpSUlJSUlKGhoaGhoaGvr6+vr6+8vLy8vLzKysrKysrX19fX19fX5eXl5eXl8vLy8vLy////////AAAAAExhdmM1Ny44OQAAAAAAAAAAAAAAACQCgAAAAAAAAAVY82AhbwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/+MYxAALACwAAP/AADwQKVE9YWDGPkQWpT66yk4+zIiYPoTUaT3tnU487uNhOvEmQDaCm1Yz1c6DPjbs6zdZVBk0pdGpMzxF/+MYxA8L0DU0AP+0ANkwmYaAMkOKDDjmYoMtwNMyDxMzDHE/MEsLow9AtDnBlQgDhTx+Eye0GgMHoCyDC8gUswJcMVMABBGj/+MYxBoK4DVpQP8iAtVmDk7LPgi8wvDzI4/MWAwK1T7rxOQwtsItMMQBazAowc4wZMC5MF4AeQAGDpruNuMEzyfjLBJhACU+/+MYxCkJ4DVcAP8MAO9J9THVg6oxRMGNMIqCCTAEwzwwBkINOPAs/iwjgBnMepYyId0PhWo+80PXMVsBFzD/AiwwfcK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VVVVVVVVVVVVVVVVVVV/+MYxPQL0DVcAP+0AFVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV';
Expand Down Expand Up @@ -312,6 +313,8 @@ function initSetting() {
catch (e) { }
}

initVisibilityListener();

// Forced request File System to check if have temp archive
if (setting['store-in-fs'] && requestFileSystem) {
requestFileSystem(window.TEMPORARY, 1024 * 1024 * 1024, ehDownloadFS.initCheckerHandler, ehDownloadFS.errorHandler);
Expand Down Expand Up @@ -1914,50 +1917,57 @@ function initEHDownload() {
getAllPagesURL();

// init playing music
if (setting['play-silent-music']) {
if (setting['play-silent-music'] && !emptyAudio) {
emptyAudio = new Audio(emptyAudioFile);
emptyAudio.loop = true;
}
}

function initVisibilityListener() {
var hidden, visibilityChange;
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
}
else if (typeof document.mozHidden !== 'undefined') {
hidden = 'mozHidden';
visibilityChange = 'mozvisibilitychange';
}
else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}

var hidden, visibilityChange;
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
hidden = 'hidden';
visibilityChange = 'visibilitychange';
var visibilityChangeHandler = function (isHidden) {
if (typeof isHidden !== 'boolean') {
isHidden = document[hidden];
}
else if (typeof document.mozHidden !== 'undefined') {
hidden = 'mozHidden';
visibilityChange = 'mozvisibilitychange';
visibleState = !isHidden;
if (!emptyAudio) {
return;
}
else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden';
visibilityChange = 'webkitvisibilitychange';
}

var visibilityChangeHandler = function(isHidden) {
if (typeof isHidden !== 'boolean') {
isHidden = document[hidden];
}
if (isHidden && ((isDownloading && !isPausing) || isSaving)) {
emptyAudio.play();
}
else {
emptyAudio.pause();
}
};

if (visibilityChange) {
window.addEventListener(visibilityChange, visibilityChangeHandler);
if (isHidden && ((isDownloading && !isPausing) || isSaving)) {
emptyAudio.play();
}
else {
window.addEventListener('focus', function() {
visibilityChangeHandler(false);
});
window.addEventListener('blur', function() {
visibilityChangeHandler(true);
});
emptyAudio.pause();
getImageLimits();
}
};

visibilityChangeHandler();
if (visibilityChange) {
window.addEventListener(visibilityChange, visibilityChangeHandler);
}
else {
window.addEventListener('focus', function () {
visibilityChangeHandler(false);
});
window.addEventListener('blur', function () {
visibilityChangeHandler(true);
});
}

visibilityChangeHandler();
}

function initProgressTable(){
Expand Down Expand Up @@ -2416,6 +2426,9 @@ function showSettings() {
}

function getImageLimits(forced, host){
if (!visibleState) {
return;
}
var host = host || location.hostname;
if (host === 'exhentai.org') {
host = 'e-hentai.org';
Expand All @@ -2440,12 +2453,16 @@ function getImageLimits(forced, host){
if (responseText.indexOf('as your account has been suspended') >= 0) {
preData.suspended = true;
}
else if (responseText.indexOf('Your IP address has been temporarily banned') >= 0) {
preData.ipBanned = true;
}
else {
var data = responseText.match(ehDownloadRegex.imageLimits);
if (!data || data.length < 3) return;
preData.cur = data[1];
preData.total = data[2];
delete preData.suspended;
delete preData.ipBanned;
}
localStorage.setItem('ehd-image-limits-' + host, JSON.stringify(preData));
showImageLimits();
Expand All @@ -2461,6 +2478,9 @@ function showImageLimits(){
if (curData.suspended) {
return '<a href="https://forums.e-hentai.org/" target="_blank" style="color: #f00">! Account Suspended !</span>';
}
if (curData.ipBanned) {
return '<a href="https://e-hentai.org/home.php" target="_blank" style="color: #f00">! IP Banned !</span>';
}
return curData.cur + '/' + curData.total;
});

Expand Down

0 comments on commit 3802022

Please sign in to comment.