github.com/primecitizens/pcz/std@v0.2.1/plat/js/webext/contentscripts/apis_js_wasm.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 4 package contentscripts 5 6 import ( 7 "github.com/primecitizens/pcz/std/ffi/js" 8 "github.com/primecitizens/pcz/std/plat/js/webext/contentscripts/bindings" 9 "github.com/primecitizens/pcz/std/plat/js/webext/extensiontypes" 10 ) 11 12 type RunAt uint32 13 14 const ( 15 _ RunAt = iota 16 17 RunAt_DOCUMENT_IDLE 18 RunAt_DOCUMENT_START 19 RunAt_DOCUMENT_END 20 ) 21 22 func (RunAt) FromRef(str js.Ref) RunAt { 23 return RunAt(bindings.ConstOfRunAt(str)) 24 } 25 26 func (x RunAt) String() (string, bool) { 27 switch x { 28 case RunAt_DOCUMENT_IDLE: 29 return "document_idle", true 30 case RunAt_DOCUMENT_START: 31 return "document_start", true 32 case RunAt_DOCUMENT_END: 33 return "document_end", true 34 default: 35 return "", false 36 } 37 } 38 39 type ContentScript struct { 40 // Matches is "ContentScript.matches" 41 // 42 // Optional 43 Matches js.Array[js.String] 44 // ExcludeMatches is "ContentScript.exclude_matches" 45 // 46 // Optional 47 ExcludeMatches js.Array[js.String] 48 // Css is "ContentScript.css" 49 // 50 // Optional 51 Css js.Array[js.String] 52 // Js is "ContentScript.js" 53 // 54 // Optional 55 Js js.Array[js.String] 56 // AllFrames is "ContentScript.all_frames" 57 // 58 // Optional 59 // 60 // NOTE: FFI_USE_AllFrames MUST be set to true to make this field effective. 61 AllFrames bool 62 // MatchOriginAsFallback is "ContentScript.match_origin_as_fallback" 63 // 64 // Optional 65 // 66 // NOTE: FFI_USE_MatchOriginAsFallback MUST be set to true to make this field effective. 67 MatchOriginAsFallback bool 68 // MatchAboutBlank is "ContentScript.match_about_blank" 69 // 70 // Optional 71 // 72 // NOTE: FFI_USE_MatchAboutBlank MUST be set to true to make this field effective. 73 MatchAboutBlank bool 74 // IncludeGlobs is "ContentScript.include_globs" 75 // 76 // Optional 77 IncludeGlobs js.Array[js.String] 78 // ExcludeGlobs is "ContentScript.exclude_globs" 79 // 80 // Optional 81 ExcludeGlobs js.Array[js.String] 82 // RunAt is "ContentScript.run_at" 83 // 84 // Optional 85 RunAt RunAt 86 // World is "ContentScript.world" 87 // 88 // Optional 89 World extensiontypes.ExecutionWorld 90 91 FFI_USE_AllFrames bool // for AllFrames. 92 FFI_USE_MatchOriginAsFallback bool // for MatchOriginAsFallback. 93 FFI_USE_MatchAboutBlank bool // for MatchAboutBlank. 94 95 FFI_USE bool 96 } 97 98 // FromRef calls UpdateFrom and returns a ContentScript with all fields set. 99 func (p ContentScript) FromRef(ref js.Ref) ContentScript { 100 p.UpdateFrom(ref) 101 return p 102 } 103 104 // New creates a new ContentScript in the application heap. 105 func (p ContentScript) New() js.Ref { 106 return bindings.ContentScriptJSLoad( 107 js.Pointer(&p), js.True, 0, 108 ) 109 } 110 111 // UpdateFrom copies value of all fields of the heap object to p. 112 func (p *ContentScript) UpdateFrom(ref js.Ref) { 113 bindings.ContentScriptJSStore( 114 js.Pointer(p), ref, 115 ) 116 } 117 118 // Update writes all fields of the p to the heap object referenced by ref. 119 func (p *ContentScript) Update(ref js.Ref) { 120 bindings.ContentScriptJSLoad( 121 js.Pointer(p), js.False, ref, 122 ) 123 } 124 125 // FreeMembers frees fields with heap reference, if recursive is true 126 // free all heap references reachable from p. 127 func (p *ContentScript) FreeMembers(recursive bool) { 128 js.Free( 129 p.Matches.Ref(), 130 p.ExcludeMatches.Ref(), 131 p.Css.Ref(), 132 p.Js.Ref(), 133 p.IncludeGlobs.Ref(), 134 p.ExcludeGlobs.Ref(), 135 ) 136 p.Matches = p.Matches.FromRef(js.Undefined) 137 p.ExcludeMatches = p.ExcludeMatches.FromRef(js.Undefined) 138 p.Css = p.Css.FromRef(js.Undefined) 139 p.Js = p.Js.FromRef(js.Undefined) 140 p.IncludeGlobs = p.IncludeGlobs.FromRef(js.Undefined) 141 p.ExcludeGlobs = p.ExcludeGlobs.FromRef(js.Undefined) 142 } 143 144 type ManifestKeys struct { 145 // ContentScripts is "ManifestKeys.content_scripts" 146 // 147 // Optional 148 ContentScripts js.Array[ContentScript] 149 150 FFI_USE bool 151 } 152 153 // FromRef calls UpdateFrom and returns a ManifestKeys with all fields set. 154 func (p ManifestKeys) FromRef(ref js.Ref) ManifestKeys { 155 p.UpdateFrom(ref) 156 return p 157 } 158 159 // New creates a new ManifestKeys in the application heap. 160 func (p ManifestKeys) New() js.Ref { 161 return bindings.ManifestKeysJSLoad( 162 js.Pointer(&p), js.True, 0, 163 ) 164 } 165 166 // UpdateFrom copies value of all fields of the heap object to p. 167 func (p *ManifestKeys) UpdateFrom(ref js.Ref) { 168 bindings.ManifestKeysJSStore( 169 js.Pointer(p), ref, 170 ) 171 } 172 173 // Update writes all fields of the p to the heap object referenced by ref. 174 func (p *ManifestKeys) Update(ref js.Ref) { 175 bindings.ManifestKeysJSLoad( 176 js.Pointer(p), js.False, ref, 177 ) 178 } 179 180 // FreeMembers frees fields with heap reference, if recursive is true 181 // free all heap references reachable from p. 182 func (p *ManifestKeys) FreeMembers(recursive bool) { 183 js.Free( 184 p.ContentScripts.Ref(), 185 ) 186 p.ContentScripts = p.ContentScripts.FromRef(js.Undefined) 187 }