Skip to content

Commit 0323db5

Browse files
authored
Merge pull request #4 from regorxxx/input_args
2 parents 4cb97f7 + 442a657 commit 0323db5

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

foo_last_list_smp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const lastList = new _lastList();
1414
// Button
1515
const buttons = {
1616
LastListButton: new columnButton(buttonTemplate, 0, "Last List", function () {
17-
lastList.run();
17+
lastList.run(null, null, null);
1818
}),
1919
};
2020

main/last_list/last_list.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ class InputError extends Error {
1010

1111

1212
function _lastList() {
13+
this.cachedUrls = [];
1314

14-
this.run = () => {
15+
this.run = (url = '', pages = 1, playlistName = 'Last List') => {
1516
try {
16-
let url;
17-
try {
18-
url = utils.InputBox(0, "Enter the URL:", "Download", '', true);
19-
} catch (e) {
20-
throw new InputError('Cancelled Input');
21-
}
22-
2317
if (!url) {
24-
throw new InputError('No URL');
18+
try {
19+
url = utils.InputBox(0, "Enter the URL:", "Download", this.cachedUrls.length ? this.cachedUrls[0] : '', true);
20+
} catch (e) {
21+
throw new InputError('Cancelled Input');
22+
}
23+
24+
if (!url) {
25+
throw new InputError('No URL');
26+
}
2527
}
2628

2729
// if url has page as parameter, set directPage to true
@@ -39,29 +41,39 @@ function _lastList() {
3941
url = url.replace(matches[0][1], "");
4042
}
4143

42-
let pages;
43-
try {
44-
pages = utils.InputBox(0, "Enter the number of pages:", "Download", '1', true);
45-
} catch (e) {
46-
throw new InputError('Cancelled Input');
47-
}
48-
pages = parseInt(pages);
49-
if (isNaN(pages) || pages < 1) {
50-
pages = 1;
51-
}
44+
if (!pages || isNaN(pages) || pages < 1) {
45+
try {
46+
pages = utils.InputBox(0, "Enter the number of pages:", "Download", '1', true);
47+
} catch (e) {
48+
throw new InputError('Cancelled Input');
49+
}
5250

53-
let playlistName;
54-
try {
55-
playlistName = utils.InputBox(0, "Enter the playlist name:", "Download", 'LastList', true);
56-
} catch (e) {
57-
throw new InputError('Cancelled Input');
51+
pages = parseInt(pages);
52+
if (isNaN(pages) || pages < 1) {
53+
pages = 1;
54+
}
5855
}
5956

6057
if (!playlistName) {
61-
throw new InputError('No playlist name');
58+
try {
59+
playlistName = utils.InputBox(0, "Enter the playlist name:", "Download", 'Last List', true);
60+
} catch (e) {
61+
throw new InputError('Cancelled Input');
62+
}
63+
64+
if (!playlistName) {
65+
throw new InputError('No playlist name');
66+
}
6267
}
6368

6469
this.scrapeUrl(url, startPage, pages, playlistName);
70+
71+
// removes url from cache if it exists and slices the cache to 9 items
72+
this.cachedUrls = this.cachedUrls.filter((cachedUrl) => {
73+
return cachedUrl !== url;
74+
}).slice(0, 9);
75+
// add url to the beginning of the cache
76+
this.cachedUrls.unshift(url);
6577
} catch (e) {
6678
if (e instanceof InputError) {
6779
// do nothing

0 commit comments

Comments
 (0)