6 JavaScript函数共用
Xwite edited this page 2024-04-10 21:48:46 +08:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

为了减少重复的JavaScript代码提供常用的函数共用方法

推荐使用jsLib规则片段只会执行一次变量函数会保存在JS上下文中

存放函数片段

  • 在线文件托管
  • 本地文件
  • 书源的备注字段bookSourceComment
  • 书源的jsLib字段

阅读版本需要大于3.23.0503
需要注意的是如果直接填写jsjsLib的js上下文中没有阅读定义的变量,如java, cache等,需要使用如下方法获取。例

function method(arg) {
  // 从上下文获取java cache source 变量
  const { java, source, cookie } = this;
  /* Android L版本不支持解构
  let java = this.java, source = this.source
   */
  java.log(arg + "调用java.log")
}

执行

eval函数

eval() 函数会将传入的字符串当做 JavaScript 代码进行执行,不过速度较慢,有可能的话建议使用{{}}规则

eval(String(source.bookSourceComment))
eval(String(java.importScript(url)))

method(arg)

{{}}规则

  • 注意书源的书籍列表规则目录列表规则不支持该规则
  • 阅读内部会将该规则替换成表达式的返回值,相当于手动复制粘贴
{{source.bookSourceComment}}
{{java.importScript(url)}}

method(arg)

jsLib规则

method(arg)