opencc-rust-lib

opencc-rust-wasm decode

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/448529/1074680/opencc-rust-lib.js

  1. // ==UserScript==
  2. // @name opencc-rust-lib
  3. // @namespace Forbidden Siren
  4. // @version 0.6.0
  5. // @description opencc-rust-wasm decode
  6. // ==/UserScript==
  7. let wasm;
  8. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  9.  
  10. cachedTextDecoder.decode();
  11.  
  12. let cachegetUint8Memory0 = null;
  13. function getUint8Memory0() {
  14. if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
  15. cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
  16. }
  17. return cachegetUint8Memory0;
  18. }
  19.  
  20. function getStringFromWasm0(ptr, len) {
  21. return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
  22. }
  23.  
  24. let WASM_VECTOR_LEN = 0;
  25.  
  26. let cachedTextEncoder = new TextEncoder('utf-8');
  27.  
  28. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  29. ? function (arg, view) {
  30. return cachedTextEncoder.encodeInto(arg, view);
  31. }
  32. : function (arg, view) {
  33. const buf = cachedTextEncoder.encode(arg);
  34. view.set(buf);
  35. return {
  36. read: arg.length,
  37. written: buf.length
  38. };
  39. });
  40.  
  41. function passStringToWasm0(arg, malloc, realloc) {
  42.  
  43. if (realloc === undefined) {
  44. const buf = cachedTextEncoder.encode(arg);
  45. const ptr = malloc(buf.length);
  46. getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
  47. WASM_VECTOR_LEN = buf.length;
  48. return ptr;
  49. }
  50.  
  51. let len = arg.length;
  52. let ptr = malloc(len);
  53.  
  54. const mem = getUint8Memory0();
  55.  
  56. let offset = 0;
  57.  
  58. for (; offset < len; offset++) {
  59. const code = arg.charCodeAt(offset);
  60. if (code > 0x7F) break;
  61. mem[ptr + offset] = code;
  62. }
  63.  
  64. if (offset !== len) {
  65. if (offset !== 0) {
  66. arg = arg.slice(offset);
  67. }
  68. ptr = realloc(ptr, len, len = offset + arg.length * 3);
  69. const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
  70. const ret = encodeString(arg, view);
  71.  
  72. offset += ret.written;
  73. }
  74.  
  75. WASM_VECTOR_LEN = offset;
  76. return ptr;
  77. }
  78.  
  79. let cachegetInt32Memory0 = null;
  80. function getInt32Memory0() {
  81. if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
  82. cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
  83. }
  84. return cachegetInt32Memory0;
  85. }
  86. /**
  87. */
  88. class Converter {
  89.  
  90. static __wrap(ptr) {
  91. const obj = Object.create(Converter.prototype);
  92. obj.ptr = ptr;
  93.  
  94. return obj;
  95. }
  96.  
  97. free() {
  98. const ptr = this.ptr;
  99. this.ptr = 0;
  100.  
  101. wasm.__wbg_converter_free(ptr);
  102. }
  103. /**
  104. * @param {string} data
  105. * @returns {string}
  106. */
  107. async convert(data) {
  108. try {
  109. const retptr = wasm.__wbindgen_export_2.value - 16;
  110. wasm.__wbindgen_export_2.value = retptr;
  111. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  112. var len0 = WASM_VECTOR_LEN;
  113. wasm.converter_convert(retptr, this.ptr, ptr0, len0);
  114. var r0 = getInt32Memory0()[retptr / 4 + 0];
  115. var r1 = getInt32Memory0()[retptr / 4 + 1];
  116. return getStringFromWasm0(r0, r1);
  117. } finally {
  118. wasm.__wbindgen_export_2.value += 16;
  119. wasm.__wbindgen_free(r0, r1);
  120. }
  121. }
  122. /**
  123. */
  124. delete() {
  125. wasm.converter_delete(this.ptr);
  126. }
  127. }
  128. /**
  129. */
  130. class ConverterBuild {
  131.  
  132. static __wrap(ptr) {
  133. const obj = Object.create(ConverterBuild.prototype);
  134. obj.ptr = ptr;
  135.  
  136. return obj;
  137. }
  138.  
  139. free() {
  140. const ptr = this.ptr;
  141. this.ptr = 0;
  142.  
  143. wasm.__wbg_converterbuild_free(ptr);
  144. }
  145. /**
  146. * @returns {ConverterBuild}
  147. */
  148. static new() {
  149. var ret = wasm.converterbuild_new();
  150. return ConverterBuild.__wrap(ret);
  151. }
  152. /**
  153. * @param {string} data
  154. */
  155. adddict(data) {
  156. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  157. var len0 = WASM_VECTOR_LEN;
  158. wasm.converterbuild_adddict(this.ptr, ptr0, len0);
  159. }
  160. /**
  161. */
  162. group() {
  163. wasm.converterbuild_group(this.ptr);
  164. }
  165. /**
  166. * @returns {Converter}
  167. */
  168. build() {
  169. var ret = wasm.converterbuild_build(this.ptr);
  170. return Converter.__wrap(ret);
  171. }
  172. }
  173.  
  174. async function load(module, imports) {
  175. if (typeof Response === 'function' && module instanceof Response) {
  176.  
  177. if (typeof WebAssembly.instantiateStreaming === 'function') {
  178. try {
  179. return await WebAssembly.instantiateStreaming(module, imports);
  180.  
  181. } catch (e) {
  182. if (module.headers.get('Content-Type') != 'application/wasm') {
  183. console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
  184.  
  185. } else {
  186. throw e;
  187. }
  188. }
  189. }
  190.  
  191. const bytes = await module.arrayBuffer();
  192. return await WebAssembly.instantiate(bytes, imports);
  193.  
  194. } else {
  195.  
  196. const instance = await WebAssembly.instantiate(module, imports);
  197.  
  198. if (instance instanceof WebAssembly.Instance) {
  199. return { instance, module };
  200.  
  201. } else {
  202. return instance;
  203. }
  204. }
  205. }
  206.  
  207. async function init(input) {
  208. const imports = {};
  209. imports.wbg = {};
  210. imports.wbg.__wbindgen_throw = function(arg0, arg1) {
  211. throw new Error(getStringFromWasm0(arg0, arg1));
  212. };
  213. const { instance, module } = await load(await input, imports);
  214.  
  215. wasm = instance.exports;
  216. init.__wbindgen_wasm_module = module;
  217.  
  218. return wasm;
  219. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址