Skip to content

Commit a223731

Browse files
committed
refactor(monkey): separate GM_apis into individual files and update copymanga-enhance
- Extract GM_addStyle, GM_getResourceText, GM_setClipboard, GM_xmlhttpRequest into separate polyfill files. - Update copymanga-enhance to use resource and GM_getResourceText for vue loading. - Add name and description to copymanga-enhance banner.json. - Remove global types/monkey.d.ts in favor of per-file declarations.
1 parent 18b9a99 commit a223731

14 files changed

Lines changed: 95 additions & 52 deletions

File tree

‎src/monkey/copymanga-enhance/banner.json‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
2-
"name": "Enhance the copy manga site",
2+
"name": "快乐看拷贝",
3+
"name:en": "Enhance the copy manga site",
4+
"description": "对开布局、支持带鱼屏、自适应图片高度、快捷翻页、支持键盘操作",
5+
"description:en": "support ultra-wide screen, adaptive image height, quick page turning, keyboard operation",
36
"match": [
47
"https://copymanga.com/comic/*/chapter/*",
58
"https://*.copymanga.com/comic/*/chapter/*",
@@ -26,9 +29,10 @@
2629
],
2730
"connect": "unpkg.com",
2831
"require": "https://unpkg.com/vue@3/dist/vue.global.prod.js",
32+
"resource": "vue https://unpkg.com/vue@3/dist/vue.global.prod.js",
2933
"run-at": "document-end",
3034
"grant": [
31-
"GM_xmlhttpRequest",
35+
"GM_getResourceText",
3236
"GM_addStyle",
3337
"GM_setValue",
3438
"GM_getValue"

‎src/monkey/copymanga-enhance/index.ts‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@ import html from './template.html'
22
import { render } from './scripts/newPage'
33
import { getPageInfo, refreshImage, windowScrollTo } from './scripts/old'
44
import storage from './scripts/storage'
5+
import GM_getResourceText from '../polyfill/GM_getResourceText'
56

6-
const renderNewPage = () => {
7+
const renderNewPage = async () => {
78
console.log('PageInfo:', storage.pageInfo)
89
windowScrollTo(0)
910
document.body.innerHTML = html
10-
GM_xmlhttpRequest({
11-
method: 'GET',
12-
url: 'https://unpkg.com/vue@3/dist/vue.global.prod.js',
13-
onload: (res) => {
14-
const script = document.createElement('script')
15-
script.textContent = res.responseText
16-
document.head.appendChild(script)
17-
setTimeout(() => {
18-
render()
19-
}, 50)
20-
},
21-
})
22-
// render()
11+
const textContent = await GM_getResourceText('vue')
12+
const script = document.createElement('script')
13+
script.textContent = textContent
14+
document.head.appendChild(script)
15+
setTimeout(() => {
16+
render()
17+
}, 50)
2318
}
2419

2520
setTimeout(() => {

‎src/monkey/copymanga-enhance/scripts/newPage/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApp, h } from './vue'
22
import App from './component/App'
33
import tailwindCss from './tailwind.css'
4+
import GM_addStyle from '../../../polyfill/GM_addStyle'
45

56
export const render = () => {
67
const app = createApp({

‎src/monkey/copymanga-enhance/scripts/storage.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import GM_getValue from "../../polyfill/GM_getValue"
2+
import GM_setValue from "../../polyfill/GM_setValue"
13
import { chapter, comic } from "./constant"
24
import { DirectionMode } from "./newPage/constant"
35
import { PageInfo } from "./types"

‎src/monkey/gitlab-enhance/merge_requests/template.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import GM_addStyle from '../../polyfill/GM_addStyle'
22
import * as templateUtils from '../../../../packages/MdGenerator/index'
33
import templateCss from './template.css'
44
import { getButtonElement } from '../utils'
5+
import GM_setClipboard from '../../polyfill/GM_setClipboard'
56

67
enum BRANCH_TYPE {
78
FEATURE,

‎src/monkey/polyfill/GM.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import '../../../types/monkey.d.ts'
21
import GM_addStyle from './GM_addStyle'
32

3+
declare global {
4+
interface Window {
5+
GM: {
6+
addStyle: typeof GM_addStyle,
7+
};
8+
}
9+
}
10+
411
if (typeof window.GM === 'undefined') {
512
window.GM = {
613
addStyle: GM_addStyle,
714
}
815
}
916

17+
window.GM.addStyle ??= GM_addStyle
18+
1019
export default window.GM

‎src/monkey/polyfill/GM_addStyle.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import '../../../types/monkey.d.ts'
1+
declare global {
2+
interface Window {
3+
GM_addStyle(cssContent: string): any;
4+
}
5+
}
6+
27

38
if (typeof window.GM_addStyle === 'undefined') {
4-
window.GM_addStyle = function GM_addStyle(cssContent: string) {
9+
window.GM_addStyle = function GM_addStyle(cssContent) {
510
const head = document.getElementsByTagName('head')[0]
611
if (head) {
712
const styleElement = document.createElement('style')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare global {
2+
interface Window {
3+
GM_getResourceText(name: string): Promise<string>;
4+
}
5+
}
6+
7+
export default window.GM_getResourceText

‎src/monkey/polyfill/GM_getValue.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare global {
2+
interface Window {
3+
GM_getValue<T>(key: string, defaultValue: T): T;
4+
}
5+
}
6+
7+
export default window.GM_getValue
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
declare global {
2+
interface Window {
3+
GM_setClipboard(content: string, type: 'text' | 'html', callback: () => any): Promise<never>;
4+
}
5+
}
6+
7+
export default window.GM_setClipboard

0 commit comments

Comments
 (0)