Skip to content

Commit 22925cb

Browse files
committed
✨ GM_getResourceURL支持获取blob url #84
1 parent 0d0f4db commit 22925cb

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

example/gm_add_element.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ const el = GM_addElement(document.querySelector('.BorderGrid-cell'), "img", {
1212
src: "https://bbs.tampermonkey.net.cn/uc_server/avatar.php?uid=4&size=small&ts=1"
1313
});
1414

15-
console.log(el);
15+
console.log(el);

example/gm_get_resource.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// ==UserScript==
2+
// @name gm get resource
3+
// @namespace https://bbs.tampermonkey.net.cn/
4+
// @version 0.1.0
5+
// @description 通过@resource引用资源,这个资源会被管理器进行缓存,不可修改
6+
// @author You
7+
// @match https://bbs.tampermonkey.net.cn/
8+
// @resource bbs https://bbs.tampermonkey.net.cn/
9+
// @grant GM_getResourceURL
10+
// @grant GM_getResourceText
11+
// ==/UserScript==
12+
13+
14+
console.log(GM_getResourceURL("bbs"));
15+
console.log(GM_getResourceURL("bbs", false));
16+
console.log(GM_getResourceURL("bbs", true));
17+
console.log(GM_getResourceText("bbs"));

src/runtime/content/gm_api.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import { Channel, ChannelHandler } from "@App/app/message/channel";
55
import MessageContent from "@App/app/message/content";
66
import { MessageManager } from "@App/app/message/message";
77
import { ScriptRunResouce } from "@App/app/repo/scripts";
8-
import { blobToBase64, getMetadataStr } from "@App/pkg/utils/script";
8+
import {
9+
base64ToBlob,
10+
blobToBase64,
11+
getMetadataStr,
12+
} from "@App/pkg/utils/script";
913
import { v4 as uuidv4 } from "uuid";
1014
import { ValueUpdateData } from "./exec_script";
1115

@@ -518,12 +522,15 @@ export default class GMApi {
518522
}
519523

520524
@GMContext.API()
521-
GM_getResourceURL(name: string): string | undefined {
525+
GM_getResourceURL(name: string, isBlobUrl?: boolean): string | undefined {
522526
if (!this.scriptRes.resource) {
523527
return undefined;
524528
}
525529
const r = this.scriptRes.resource[name];
526530
if (r) {
531+
if (isBlobUrl) {
532+
return URL.createObjectURL(base64ToBlob(r.base64));
533+
}
527534
return r.base64;
528535
}
529536
return undefined;

src/types/scriptcat.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ declare function GM_log(
7878

7979
declare function GM_getResourceText(name: string): string | undefined;
8080

81-
declare function GM_getResourceURL(name: string): string | undefined;
81+
declare function GM_getResourceURL(
82+
name: string,
83+
isBlobUrl?: boolean = false
84+
): string | undefined;
8285

8386
declare function GM_registerMenuCommand(
8487
name: string,

0 commit comments

Comments
 (0)