🏠 Home 

opencc-rust-lib

opencc-rust-wasm decode

สคริปต์นี้ไม่ควรถูกติดตั้งโดยตรง มันเป็นคลังสำหรับสคริปต์อื่น ๆ เพื่อบรรจุด้วยคำสั่งเมทา // @require https://update.greasyfork.org/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. cachedTextDecoder.decode();
  10. let cachegetUint8Memory0 = null;
  11. function getUint8Memory0() {
  12. if (cachegetUint8Memory0 === null || cachegetUint8Memory0.buffer !== wasm.memory.buffer) {
  13. cachegetUint8Memory0 = new Uint8Array(wasm.memory.buffer);
  14. }
  15. return cachegetUint8Memory0;
  16. }
  17. function getStringFromWasm0(ptr, len) {
  18. return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
  19. }
  20. let WASM_VECTOR_LEN = 0;
  21. let cachedTextEncoder = new TextEncoder('utf-8');
  22. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  23. ? function (arg, view) {
  24. return cachedTextEncoder.encodeInto(arg, view);
  25. }
  26. : function (arg, view) {
  27. const buf = cachedTextEncoder.encode(arg);
  28. view.set(buf);
  29. return {
  30. read: arg.length,
  31. written: buf.length
  32. };
  33. });
  34. function passStringToWasm0(arg, malloc, realloc) {
  35. if (realloc === undefined) {
  36. const buf = cachedTextEncoder.encode(arg);
  37. const ptr = malloc(buf.length);
  38. getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
  39. WASM_VECTOR_LEN = buf.length;
  40. return ptr;
  41. }
  42. let len = arg.length;
  43. let ptr = malloc(len);
  44. const mem = getUint8Memory0();
  45. let offset = 0;
  46. for (; offset < len; offset++) {
  47. const code = arg.charCodeAt(offset);
  48. if (code > 0x7F) break;
  49. mem[ptr + offset] = code;
  50. }
  51. if (offset !== len) {
  52. if (offset !== 0) {
  53. arg = arg.slice(offset);
  54. }
  55. ptr = realloc(ptr, len, len = offset + arg.length * 3);
  56. const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
  57. const ret = encodeString(arg, view);
  58. offset += ret.written;
  59. }
  60. WASM_VECTOR_LEN = offset;
  61. return ptr;
  62. }
  63. let cachegetInt32Memory0 = null;
  64. function getInt32Memory0() {
  65. if (cachegetInt32Memory0 === null || cachegetInt32Memory0.buffer !== wasm.memory.buffer) {
  66. cachegetInt32Memory0 = new Int32Array(wasm.memory.buffer);
  67. }
  68. return cachegetInt32Memory0;
  69. }
  70. /**
  71. */
  72. class Converter {
  73. static __wrap(ptr) {
  74. const obj = Object.create(Converter.prototype);
  75. obj.ptr = ptr;
  76. return obj;
  77. }
  78. free() {
  79. const ptr = this.ptr;
  80. this.ptr = 0;
  81. wasm.__wbg_converter_free(ptr);
  82. }
  83. /**
  84. * @param {string} data
  85. * @returns {string}
  86. */
  87. async convert(data) {
  88. try {
  89. const retptr = wasm.__wbindgen_export_2.value - 16;
  90. wasm.__wbindgen_export_2.value = retptr;
  91. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  92. var len0 = WASM_VECTOR_LEN;
  93. wasm.converter_convert(retptr, this.ptr, ptr0, len0);
  94. var r0 = getInt32Memory0()[retptr / 4 + 0];
  95. var r1 = getInt32Memory0()[retptr / 4 + 1];
  96. return getStringFromWasm0(r0, r1);
  97. } finally {
  98. wasm.__wbindgen_export_2.value += 16;
  99. wasm.__wbindgen_free(r0, r1);
  100. }
  101. }
  102. /**
  103. */
  104. delete() {
  105. wasm.converter_delete(this.ptr);
  106. }
  107. }
  108. /**
  109. */
  110. class ConverterBuild {
  111. static __wrap(ptr) {
  112. const obj = Object.create(ConverterBuild.prototype);
  113. obj.ptr = ptr;
  114. return obj;
  115. }
  116. free() {
  117. const ptr = this.ptr;
  118. this.ptr = 0;
  119. wasm.__wbg_converterbuild_free(ptr);
  120. }
  121. /**
  122. * @returns {ConverterBuild}
  123. */
  124. static new() {
  125. var ret = wasm.converterbuild_new();
  126. return ConverterBuild.__wrap(ret);
  127. }
  128. /**
  129. * @param {string} data
  130. */
  131. adddict(data) {
  132. var ptr0 = passStringToWasm0(data, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  133. var len0 = WASM_VECTOR_LEN;
  134. wasm.converterbuild_adddict(this.ptr, ptr0, len0);
  135. }
  136. /**
  137. */
  138. group() {
  139. wasm.converterbuild_group(this.ptr);
  140. }
  141. /**
  142. * @returns {Converter}
  143. */
  144. build() {
  145. var ret = wasm.converterbuild_build(this.ptr);
  146. return Converter.__wrap(ret);
  147. }
  148. }
  149. async function load(module, imports) {
  150. if (typeof Response === 'function' && module instanceof Response) {
  151. if (typeof WebAssembly.instantiateStreaming === 'function') {
  152. try {
  153. return await WebAssembly.instantiateStreaming(module, imports);
  154. } catch (e) {
  155. if (module.headers.get('Content-Type') != 'application/wasm') {
  156. 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);
  157. } else {
  158. throw e;
  159. }
  160. }
  161. }
  162. const bytes = await module.arrayBuffer();
  163. return await WebAssembly.instantiate(bytes, imports);
  164. } else {
  165. const instance = await WebAssembly.instantiate(module, imports);
  166. if (instance instanceof WebAssembly.Instance) {
  167. return { instance, module };
  168. } else {
  169. return instance;
  170. }
  171. }
  172. }
  173. async function init(input) {
  174. const imports = {};
  175. imports.wbg = {};
  176. imports.wbg.__wbindgen_throw = function(arg0, arg1) {
  177. throw new Error(getStringFromWasm0(arg0, arg1));
  178. };
  179. const { instance, module } = await load(await input, imports);
  180. wasm = instance.exports;
  181. init.__wbindgen_wasm_module = module;
  182. return wasm;
  183. }