|
| 1 | +## 导入 |
| 2 | + |
| 3 | +### NPM |
| 4 | + |
| 5 | +```js |
| 6 | +import { initComicReader } from '@hymbz/comic-read-script/dist/umd.js' |
| 7 | + |
| 8 | +const ComicReader = initComicReader(); |
| 9 | +ComicReader.open(['/1.png', '/2.png', '/3.png']); |
| 10 | +``` |
| 11 | + |
| 12 | +### CDN |
| 13 | + |
| 14 | +```html |
| 15 | +<script src="https://cdn.jsdelivr.net/npm/@hymbz/comic-read-script/ComicReader.umd.js"></script> |
| 16 | +<script> |
| 17 | + const { initComicReader } = window.ComicReadScript; |
| 18 | + const ComicReader = initComicReader(); |
| 19 | + ComicReader.open(['/1.png', '/2.png', '/3.png']); |
| 20 | +</script> |
| 21 | +``` |
| 22 | + |
| 23 | +## 配置 |
| 24 | + |
| 25 | +正常无需任何配置,就已经可以使用阅读模式的大部分功能了,但仍有一些功能需要提供油猴 API 才能使用。 |
| 26 | + |
| 27 | +### GM.getValue / GM.setValue |
| 28 | + |
| 29 | +脚本会用这俩 API 来保存:显示语言设置、3.3M 的图片无损放大模型(模型只在用到时才会下载缓存)。另外也能顺便用这俩 API 来存储脚本的阅读配置和快捷键。 |
| 30 | + |
| 31 | +### GM_xmlhttpRequest |
| 32 | + |
| 33 | +不提供这个 API 的话就只能使用 fetch 来发起请求了,如果没有 CORS 之类的限制的话,那除了无法使用 Cotrans 翻译外好像也没什么影响。 |
| 34 | + |
| 35 | +### 使用默认配置 |
| 36 | + |
| 37 | +```js |
| 38 | +import { initComicReader, defaultConfig } from '@hymbz/comic-read-script' |
| 39 | + |
| 40 | +const ComicReader = initComicReader(defaultConfig()); |
| 41 | +``` |
| 42 | + |
| 43 | +默认配置并不会默认启用,需要手动传入。 |
| 44 | + |
| 45 | +使用 localStorage 来实现 GM.getValue 和 GM.setValue,同时配置好了阅读配置和快捷键设置的存储,并会在版本更新时自动删掉旧的配置,详见[代码](https://github.com/hymbz/ComicReadScript/blob/master/src/umd.tsx#L177-L205)。 |
| 46 | + |
| 47 | +### 自定义配置 |
| 48 | + |
| 49 | +```js |
| 50 | +import { initComicReader } from '@hymbz/comic-read-script/dist/umd.js' |
| 51 | + |
| 52 | +const getValue = (name, defaultValue) => { /* xxx */ }; |
| 53 | +const setValue = (name, value) => { /* xxx */ }; |
| 54 | +const GM_xmlhttpRequest = (details) => { /* xxx */ }; |
| 55 | + |
| 56 | +const ComicReader = initComicReader({ |
| 57 | + polyfill: { |
| 58 | + GM: { getValue, setValue }, |
| 59 | + GM_xmlhttpRequest, |
| 60 | + }, |
| 61 | + props: { |
| 62 | + option: getValue('@Option'), |
| 63 | + onOptionChange: (option) => setValue('@Option', option), |
| 64 | + hotkeys: getValue('@Hotkeys'), |
| 65 | + onHotkeysChange: (hotkeys) => setValue('@Hotkeys', hotkeys), |
| 66 | + }, |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +props 的具体定义详见 [ComicReader.umd.d.ts](https://github.com/hymbz/ComicReadScript/blob/master/ComicReader.umd.d.ts#L306-L329) 里的 MangaProps 类型。 |
| 71 | + |
| 72 | +## 方法 |
| 73 | + |
| 74 | +```js |
| 75 | +const ComicReader = initComicReader(); |
| 76 | + |
| 77 | +// 加载显示指定的图片列表。标题用于打包下载时命名压缩包,没有标题时会使用网页标题 |
| 78 | +ComicReader.open(['/1.png', '/2.png', '/3.png'], '标题'); |
| 79 | +// 关闭阅读模式 |
| 80 | +ComicReader.setProps('show', false); |
| 81 | +// 跳到指定页数(注意在双页模式下,页数不等于图片在列表里的序列数) |
| 82 | +ComicReader.goto(1); |
| 83 | +``` |
0 commit comments