11import { BgGMXhr } from "@App/pkg/utils/xhr/bg_gm_xhr" ;
22import type { IGetSender , Group } from "@Packages/message/server" ;
3+ import { mightPrepareSetClipboard , setClipboard } from "../service_worker/clipboard" ;
34
45// nativePageXHR 不需要绑定 Offscreen.GMApi 的 this,外部可以直接引用
56export const nativePageXHR = async ( details : GMSend . XHRDetails , sender : IGetSender ) => {
@@ -15,37 +16,17 @@ export const nativePageWindowOpen = (details: { url: string }): boolean => {
1516 return ! ! window . open ( details . url ) ;
1617} ;
1718
19+ export const nativePageSetClipboard = ( { data, mimetype } : { data : string ; mimetype : string } ) => {
20+ setClipboard ( data , mimetype ) ;
21+ } ;
22+
1823export default class GMApi {
1924 constructor ( private group : Group ) { }
2025
21- textarea : HTMLTextAreaElement = document . createElement ( "textarea" ) ;
22-
23- clipboardData : { type ?: string ; data : string } | undefined ;
24-
25- async setClipboard ( { data, type } : { data : string ; type : string } ) {
26- this . clipboardData = {
27- type,
28- data,
29- } ;
30- this . textarea . focus ( ) ;
31- document . execCommand ( "copy" , false , < any > null ) ;
32- }
33-
3426 init ( ) {
35- this . textarea . style . display = "none" ;
36- document . documentElement . appendChild ( this . textarea ) ;
37- document . addEventListener ( "copy" , ( e : ClipboardEvent ) => {
38- if ( ! this . clipboardData || ! e . clipboardData ) {
39- return ;
40- }
41- e . preventDefault ( ) ;
42- const { type, data } = this . clipboardData ;
43- e . clipboardData . setData ( type || "text/plain" , data ) ;
44- this . clipboardData = undefined ;
45- } ) ;
46-
27+ mightPrepareSetClipboard ( ) ;
4728 this . group . on ( "xmlHttpRequest" , nativePageXHR ) ;
4829 this . group . on ( "windowOpen" , nativePageWindowOpen ) ;
49- this . group . on ( "setClipboard" , this . setClipboard . bind ( this ) ) ;
30+ this . group . on ( "setClipboard" , nativePageSetClipboard ) ;
5031 }
5132}
0 commit comments