Skip to content

Commit

Permalink
✨ GM_getResourceURL支持获取blob url #84
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 2, 2022
1 parent 0d0f4db commit 22925cb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion example/gm_add_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const el = GM_addElement(document.querySelector('.BorderGrid-cell'), "img", {
src: "https://bbs.tampermonkey.net.cn/uc_server/avatar.php?uid=4&size=small&ts=1"
});

console.log(el);
console.log(el);
17 changes: 17 additions & 0 deletions example/gm_get_resource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ==UserScript==
// @name gm get resource
// @namespace https://bbs.tampermonkey.net.cn/
// @version 0.1.0
// @description 通过@resource引用资源,这个资源会被管理器进行缓存,不可修改
// @author You
// @match https://bbs.tampermonkey.net.cn/
// @resource bbs https://bbs.tampermonkey.net.cn/
// @grant GM_getResourceURL
// @grant GM_getResourceText
// ==/UserScript==


console.log(GM_getResourceURL("bbs"));
console.log(GM_getResourceURL("bbs", false));
console.log(GM_getResourceURL("bbs", true));
console.log(GM_getResourceText("bbs"));
11 changes: 9 additions & 2 deletions src/runtime/content/gm_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { Channel, ChannelHandler } from "@App/app/message/channel";
import MessageContent from "@App/app/message/content";
import { MessageManager } from "@App/app/message/message";
import { ScriptRunResouce } from "@App/app/repo/scripts";
import { blobToBase64, getMetadataStr } from "@App/pkg/utils/script";
import {
base64ToBlob,
blobToBase64,
getMetadataStr,
} from "@App/pkg/utils/script";
import { v4 as uuidv4 } from "uuid";
import { ValueUpdateData } from "./exec_script";

Expand Down Expand Up @@ -518,12 +522,15 @@ export default class GMApi {
}

@GMContext.API()
GM_getResourceURL(name: string): string | undefined {
GM_getResourceURL(name: string, isBlobUrl?: boolean): string | undefined {
if (!this.scriptRes.resource) {
return undefined;
}
const r = this.scriptRes.resource[name];
if (r) {
if (isBlobUrl) {
return URL.createObjectURL(base64ToBlob(r.base64));
}
return r.base64;
}
return undefined;
Expand Down
5 changes: 4 additions & 1 deletion src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ declare function GM_log(

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

declare function GM_getResourceURL(name: string): string | undefined;
declare function GM_getResourceURL(
name: string,
isBlobUrl?: boolean = false
): string | undefined;

declare function GM_registerMenuCommand(
name: string,
Expand Down

0 comments on commit 22925cb

Please sign in to comment.