-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathplugin.resume.js
More file actions
71 lines (63 loc) · 2.05 KB
/
Copy pathplugin.resume.js
File metadata and controls
71 lines (63 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { EVENTS } from '../BookReader/events.js';
import { BookReaderPlugin } from '../BookReaderPlugin.js';
import * as docCookies from '../util/docCookies.js';
/* global BookReader */
/** @deprecated Exposed for backward compatibility */
BookReader.docCookies = docCookies;
/**
* Plugin to remember the current page number in a cookie
*/
export class ResumePlugin extends BookReaderPlugin {
options = {
enabled: true,
/** @type {string|null} eg '/', '/details/id' */
cookiePath: null,
}
/** @override */
init() {
if (this.options.enabled) {
this.br.bind(EVENTS.fragmentChange, () => {
const params = this.br.paramsFromCurrent();
this.updateResumeValue(params.index);
});
}
}
/**
* Gets page resume value, for remembering reader's page
* Can be overridden for different implementation
*
* @return {number|null}
*/
getResumeValue() {
const val = BookReader.docCookies.getItem('br-resume');
if (val !== null) return parseInt(val);
else return null;
}
/**
* Return cookie path using pathname up to /page/... or /mode/...
* using window.location.pathname for urlPathPart:
* - matches encoding
* - ignores querystring part
* - ignores fragment part (after #)
* @param {string} urlPathPart - window.location.pathname
*/
getCookiePath(urlPathPart) {
return urlPathPart.match('.+?(?=/page/|/mode/|$)')[0];
}
/**
* Sets page resume value, for remembering reader's page
* Can be overridden for different implementation
*
* @param {Number} index leaf index
* @param {string} [cookieName]
*/
updateResumeValue(index, cookieName) {
const ttl = new Date(+new Date + 12096e5); // 2 weeks
// For multiple files in item, leave cookiePath blank
// It's likely we can remove cookiePath using getCookiePath()
const path = this.options.cookiePath
|| this.getCookiePath(window.location.pathname);
BookReader.docCookies.setItem(cookieName || 'br-resume', index, ttl, path, null, false);
}
}
BookReader?.registerPlugin('resume', ResumePlugin);