Skip to content

Commit 52584ab

Browse files
committed
Remove axios, shrinking file size
1 parent 0f98bcf commit 52584ab

File tree

16 files changed

+238
-69
lines changed

16 files changed

+238
-69
lines changed

README-zh_CN.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,17 @@
44
一个基于网易云音乐的简易播放器,而且它仅仅是**一个**JavaScript文件!
55

66
## 用法
7-
首先,下载[最新版本](https://github.com/M4TEC/PenguinPlayer/releases/latest/download/player.js)```player.js```
7+
首先,下载[最新版本](https://github.com/M4TEC/PenguinPlayer/releases/latest/download/player.js)的播放器
88

99
现在,在```window```上下文中设置```penguin_id```来使用你的歌单,比如```window.penguin_id = "440401494"```.
1010

11-
然后只需要将```player.js```引入到你的页面里!
11+
然后只需要将你刚刚下载的JavaScript文件引入到你的页面里!
12+
13+
## 版本
14+
| 文件名 | 描述 |
15+
| ----- | ---- |
16+
| player.js | ```推荐``` 这个版本能在现代浏览器中使用 |
17+
| player.ie.js | 一个包含为```Internet Explorer```准备的兼容代码的版本,如果你使用IE请使用本版本 |
1218

1319
### 提示
1420
如果你正在查看预览页面,你可以通过在网址后面添加```?playlist=[你的歌单ID]```来使用你自己的歌单。

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,17 @@
66
A simple player based on Netease Cloud Music, and it only needs to load ONE JavaScript file!
77

88
## Usage
9-
First, download the [latest release](https://github.com/M4TEC/PenguinPlayer/releases/latest/download/player.js) of ```player.js``` from Releases.
9+
First, download the [latest release](https://github.com/M4TEC/PenguinPlayer/releases/latest) of the player from Releases.
1010

1111
Now, set your playlist ID by setting ```penguin_id``` in ```window``` context, for example ```window.penguin_id = "440401494"```.
1212

13-
Then just include ```player.js``` inside your page!
13+
Then just include the JavaScript file that you just downloaded inside your page!
14+
15+
## Versions
16+
| File Name | Description |
17+
| --------- | ----------- |
18+
| player.js | ```Recommend``` This version will work in modern browsers |
19+
| player.ie.js | A version that contains polyfills for ```Internet Explorer```, use this if you want to support it |
1420

1521
### Note
1622
If you are visiting the demo page, you can use your own playlist by append ```?playlist=[YOUR PLAYLIST ID HERE]``` after the URL

package-lock.json

Lines changed: 121 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A simple player based on Netease Cloud Music",
55
"main": "dist/player.js",
66
"scripts": {
7-
"build": "webpack --env production",
7+
"build": "webpack --mode=production --env production",
88
"watch": "webpack --watch",
99
"watch-ie": "webpack --watch --env ie",
1010
"build-dev": "webpack"
@@ -29,7 +29,6 @@
2929
"@babel/plugin-proposal-class-properties": "^7.13.0",
3030
"@babel/preset-env": "^7.13.5",
3131
"@babel/preset-typescript": "^7.13.0",
32-
"axios": "^0.21.1",
3332
"babel-loader": "^8.2.2",
3433
"colorthief": "^2.3.2",
3534
"core-js": "^3.9.1",
@@ -51,6 +50,7 @@
5150
"svgo-loader": "^2.2.1",
5251
"vanilla-lazyload": "^17.3.1",
5352
"webpack": "^5.24.2",
53+
"webpack-bundle-analyzer": "^4.4.0",
5454
"webpack-cli": "^4.5.0"
5555
},
5656
"dependencies": {}

src/typescript/controller.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { container as el } from "./player";
33
import { setSong as setMediaSession } from "./mediaSession";
44
import { getLyric } from "./lyric";
55
import { resetRotate, setThemeColor, volumeSlider } from "./ui";
6-
7-
import axios, { CancelTokenSource } from "axios";
8-
import { dispatchEvent } from "./event";
6+
import { dispatchEvent } from "./modules/event";
7+
import ajax from "./modules/ajax";
98

109
export let songs: Array<Song> = [];
1110
export let currentSong: number;
1211

13-
let errorAmount = 0, currentUrlToken: CancelTokenSource;
12+
let errorAmount = 0, currentUrlReq: AjaxPromise;
1413

1514
const playFailedHandler = () => {
1615
errorAmount++;
@@ -42,9 +41,8 @@ export function play(id?: number) {
4241
let song = songs[id];
4342
setThemeColor([255, 255, 255], [[0, 0, 0]]);
4443
setMediaSession(song);
45-
if (currentUrlToken) { currentUrlToken.cancel("Song changed"); }
46-
currentUrlToken = axios.CancelToken.source();
47-
axios.get(`https://gcm.tenmahw.com/song/url?id=${song.id}`, { cancelToken: currentUrlToken.token }).then((result) => {
44+
if (currentUrlReq) { currentUrlReq.cancel(); }
45+
currentUrlReq = ajax(`https://gcm.tenmahw.com/song/url?id=${song.id}`).send().then((result) => {
4846
if (result.data.code == 200) {
4947
let track = result.data.data[0];
5048
if (track.url == null) {
@@ -55,7 +53,7 @@ export function play(id?: number) {
5553
audio.play();
5654
}
5755
} else { playFailedHandler(); }
58-
}).catch((err) => !axios.isCancel(err) ? playFailedHandler() : null );
56+
}).catch(playFailedHandler);
5957
getLyric(song);
6058
resetRotate();
6159
dispatchEvent("penguinsongchange", { detail: song });

src/typescript/global.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,18 @@ interface SliderOptions {
4545
interface ThemeColorChangeEvent {
4646
color: Color
4747
palette: Color[]
48+
}
49+
50+
interface AjaxPromise extends Promise<AjaxResponse> {
51+
method(method: "GET" | "POST"): AjaxPromise
52+
url(url: string): AjaxPromise
53+
send(): AjaxPromise
54+
cancel(): AjaxPromise
55+
then(callback: any): AjaxPromise
56+
catch(callback: any): AjaxPromise
57+
finally(callback: any): AjaxPromise
58+
}
59+
interface AjaxResponse {
60+
code: number
61+
data: any
4862
}

src/typescript/lyric.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import axios, { CancelTokenSource } from "axios";
21
import { songs, currentSong } from "./controller";
32
import { print } from "./helper";
3+
import ajax from "./modules/ajax";
44
import { container as el } from "./player";
55

66
let audio: HTMLAudioElement, mainEl: HTMLHeadingElement, subEl: HTMLHeadingElement;
@@ -14,9 +14,9 @@ window.addEventListener("penguininitialized", () => {
1414
});
1515
});
1616

17-
let axiosToken: CancelTokenSource, retryTimeout: number;
17+
let lyricReq: AjaxPromise, retryTimeout: any;
1818

19-
let lrc: Array<LyricLine>, tLrc: Array<LyricLine>, lrcOffset = 0, tLrcOffset = 0, lastMain: string, lastSub: string, lrcTimeout: number, subLrcTimeout: number;
19+
let lrc: Array<LyricLine>, tLrc: Array<LyricLine>, lrcOffset = 0, tLrcOffset = 0, lastMain: string, lastSub: string, lrcTimeout: any, subLrcTimeout: any;
2020

2121
function findLrcPos(lrc: Array<LyricLine>, time: number, offset = 0): number {
2222
for (let i = offset;i < lrc.length;i++) {
@@ -63,19 +63,16 @@ export function getLyric(song: Song) {
6363
lrc = tLrc = null;
6464
lrcOffset = tLrcOffset = 0;
6565
clearTimeout(retryTimeout);
66-
if (axiosToken) {axiosToken.cancel("Fetch new lyric");}
67-
axiosToken = axios.CancelToken.source();
68-
axios.get(`https://gcm.tenmahw.com/resolve/lyric?id=${song.id}`, { cancelToken: axiosToken.token }).then((result) => {
66+
if (lyricReq) { lyricReq.cancel(); }
67+
lyricReq = ajax(`https://gcm.tenmahw.com/resolve/lyric?id=${song.id}`).then((result) => {
6968
if (result.data.lyric == null) {
7069
print(`No lyric for ${songs[currentSong].name}`);
7170
} else {
7271
lrc = result.data.lyric.lrc;
7372
tLrc = result.data.lyric.tlrc;
7473
}
75-
}).catch((err) => {
76-
if (!axios.isCancel(err)) {
77-
print("Cannot fetch lyric");
78-
retryTimeout = setTimeout(getLyric, 5000, song);
79-
}
74+
}).catch(() => {
75+
print("Cannot fetch lyric");
76+
retryTimeout = setTimeout(getLyric, 5000, song);
8077
});
8178
}

0 commit comments

Comments
 (0)