Skip to content

Commit

Permalink
chore: support GM method
Browse files Browse the repository at this point in the history
  • Loading branch information
Arylo committed Oct 28, 2024
1 parent 4ef8a4a commit 5e923f0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
8 changes: 8 additions & 0 deletions src/monkey/polyfill/GM.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const thisGlobal = window

if (typeof thisGlobal.GM === 'undefined') {
thisGlobal.GM = {} as any
}

export function getGMWindow () { return thisGlobal }

24 changes: 16 additions & 8 deletions src/monkey/polyfill/GM_addStyle.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
if (typeof (window as any).GM_addStyle == 'undefined') {
(window as any).GM_addStyle = (cssContent: string) => {
let head = document.getElementsByTagName('head')[0]
import { getGMWindow } from "./GM"

const w = getGMWindow()

if (typeof w.GM_addStyle === 'undefined') {
w.GM_addStyle = function GM_addStyle(cssContent: string) {
const head = document.getElementsByTagName('head')[0]
if (head) {
let style = document.createElement('style')
style.setAttribute('type', 'text/css')
style.textContent = cssContent
head.appendChild(style)
return style
const styleElement = document.createElement('style')
styleElement.setAttribute('type', 'text/css')
styleElement.textContent = cssContent
head.appendChild(styleElement)
return styleElement
}
return null
}
}

if (typeof w.GM.addStyle === 'undefined') {
w.GM.addStyle = GM_addStyle
}
11 changes: 10 additions & 1 deletion types/monkey.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
declare function GM_addStyle(content: string): undefined;
declare function GM_addStyle(content: string): any;
declare function GM_setValue(key: string, value: T): undefined;
declare function GM_getValue<T>(key: string, defaultValue: T): T

declare interface Window {
GM: {
addStyle: GM_addStyle,
},
GM_addStyle: GM_addStyle,
GM_setValue: GM_setValue,
GM_getValue: GM_getValue,
}

0 comments on commit 5e923f0

Please sign in to comment.