Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rate limit #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ function shouldDOMbeParsed(url, parseDOMoption, checkTypeOption) {
// Timeout for each link is 30+1 seconds
var timeout = 30000;

function check(url) {
function doCheck(url) {
var response = { status: null, document: null };
return new Promise(function (resolve, reject) {
var XMLHttpTimeout = null;
var xhr = new XMLHttpRequest();
var start = Date.now();
xhr.onreadystatechange = function (data) {
if (xhr.readyState == 4) {
log(xhr);
Expand All @@ -241,6 +242,7 @@ function check(url) {
} else {
response.status = 300;
}
response.cost = Date.now() - start;
resolve(response);
}
};
Expand All @@ -251,16 +253,68 @@ function check(url) {
} catch (e) {
console.log(e);
response.status = 0;
response.cost = Date.now() - start;
resolve(response);
}
XMLHttpTimeout = setTimeout(function () {
response.status = 408;
response.cost = Date.now() - start;
resolve(response);
xhr.abort();
}, timeout += 1000);
});
}

var urls = [];

var running = 0;

function check(url) {
const rps = Number(getOption("rps"));
if (isNaN(rps) || rps <= 0) {
return doCheck(url);
}
return new Promise((resolve, reject) => {
this.urls.push({
resolve,
reject,
url
});
this.checkNext(rps);
});
}

function checkNext(rps) {
if (this.urls.length && this.running < rps) {
let {resolve, reject, url} = this.urls.shift();
this.running++;
doCheck(url)
.then(response => {
try {
resolve(response);
} finally {
this.running--;
const timeout = 1000 - response.cost;
if (timeout > 0) {
setTimeout(() => {
checkNext(rps);
}, timeout);
} else {
checkNext(rps);
}
}
})
.catch(error => {
try {
reject(error);
} finally {
this.running--;
checkNext(rps);
}
});
}
}

function XHRisNecessary(options, url) {
if (shouldDOMbeParsed(url, options.parseDOM, options.checkType) === true || options.cache == 'false') {
return true;
Expand Down Expand Up @@ -362,6 +416,7 @@ function getOption(key) {
"adservices.google.com\n" +
"appliedsemantics.com",
checkType: "GET",
rps: "30",
cache: "false",
noFollow: "false",
parseDOM: "false",
Expand Down Expand Up @@ -398,6 +453,7 @@ function getOptions() {
var options = {};
options.blacklist = getOption("blacklist");
options.checkType = getOption("checkType");
options.rps = getOption("rps");
options.cache = getOption("cache");
options.noFollow = getOption("noFollow");
options.parseDOM = getOption("parseDOM");
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"permissions": [
"activeTab"
],
"version": "3.8.2",
"version": "3.8.2.1020",
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a mark for myself, should update to other version name.

"manifest_version": 2,
"background": {
"scripts": [
Expand Down
4 changes: 4 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ <h1 id="logo">Check My Links: Options</h1>
<p>HEAD is quicker, but sometimes doesn't reflect the true HTTP status of a page, use GET if you're getting weird results. GET is required to be able to parse the document for an element corresponding to what is after the hashtag by ID.</p>

<hr/>
<label for="rps">Requests per second: </label>
<input type="number" name="rps" id="rps" min="0" max="100" value="30" />
<p>0 means no limit.</p>
<hr />
<label for="autoCheck">AutoCheck: </label> <input type="checkbox" name="autoCheck" id="autoCheck" value="1">
<p>When enabled, if the url of the current tab changes and the page is loaded, it will automatically be checked. Valid link caching is recommended when this is enabled.</p>
<h3>Warnings</h3>
Expand Down
2 changes: 2 additions & 0 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function loadOptions() {
break;
}
}
document.getElementById("rps").value = options.rps;
});
}

Expand All @@ -64,6 +65,7 @@ function saveOptions() {
// Save selected options to localstore
bkg.setItem("blacklist", blacklistEntries.value);
bkg.setItem("checkType", requestType.children[requestType.selectedIndex].value);
bkg.setItem("rps", document.getElementById("rps").value);

if (document.getElementById("cache").checked) { bkg.setItem("cache", 'true'); } else { bkg.setItem("cache", 'false'); }
if (document.getElementById("noFollow").checked) { bkg.setItem("noFollow", 'true'); } else { bkg.setItem("noFollow", 'false'); }
Expand Down
4 changes: 4 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ QUnit.module("Options", {
QUnit.test("test the getOption function do not default", function(assert) {
window.localStorage.getItem.withArgs("blacklist").returns("http://example.com/");
window.localStorage.getItem.withArgs("checkType").returns("GET");
window.localStorage.getItem.withArgs("rps").returns("30");
window.localStorage.getItem.withArgs("cache").returns("true");
window.localStorage.getItem.withArgs("noFollow").returns("true");
window.localStorage.getItem.withArgs("parseDOM").returns("true");
Expand All @@ -344,6 +345,7 @@ QUnit.test("test the getOption function do not default", function(assert) {
window.localStorage.getItem.withArgs("optionsURL").returns("true");
assert.equal(getOption("blacklist"), "http://example.com/", "Testing the option value of blacklist is retrieved from LocalStorage");
assert.equal(getOption("checkType"), "GET", "Testing the option value of checkType is retrieved from LocalStorage");
assert.equal(getOption("rps"), "30", "Testing the option value of rps is retrieved from LocalStorage");
assert.equal(getOption("cache"), "true", "Testing the option value of cache is retrieved from LocalStorage");
assert.equal(getOption("noFollow"), "true", "Testing the option value of noFollow is retrieved from LocalStorage");
assert.equal(getOption("parseDOM"), "true", "Testing the option value of parseDOM is retrieved from LocalStorage");
Expand All @@ -366,6 +368,7 @@ QUnit.test("test the getOption function Defaults", function(assert) {
"adservices.google.com\n" +
"appliedsemantics.com", "Testing the default value of blacklist");
assert.equal(getOption("checkType"), "GET", "Testing the default value of checkType");
assert.equal(getOption("rps"), "30", "Testing the default value of rps");
assert.equal(getOption("cache"), "false", "Testing the default value of cache");
assert.equal(getOption("noFollow"), "false", "Testing the default value of noFollow");
assert.equal(getOption("parseDOM"), "false", "Testing the default value of parseDOM");
Expand All @@ -382,6 +385,7 @@ QUnit.test("test the getOptions function Defaults", function(assert) {
getOptions();
assert.equal(window.getOption.withArgs("blacklist").calledOnce, true, "getOption blacklist is called");
assert.equal(window.getOption.withArgs("checkType").calledOnce, true, "getOption checkType is called");
assert.equal(window.getOption.withArgs("rps").calledOnce, true, "getOption rps is called");
assert.equal(window.getOption.withArgs("cache").calledOnce, true, "getOption cache is called");
assert.equal(window.getOption.withArgs("noFollow").calledOnce, true, "getOption noFollow is called");
assert.equal(window.getOption.withArgs("parseDOM").calledOnce, true, "getOption parseDOM is called");
Expand Down