2023年政策修订增补工作正在进行中,欢迎参与!
  • Moegirl.ICU:萌娘百科流亡社群 581077156(QQ),欢迎对萌娘百科运营感到失望的编辑者加入
  • Moegirl.ICU:账号认领正在试运行,有意者请参照账号认领流程

User:机智的小鱼君/gadget/index.js

萌娘百科,万物皆可萌的百科全书!转载请标注来源页面的网页链接,并声明引自萌娘百科。内容不可商用。
跳转到导航 跳转到搜索

注意:在保存之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox/Safari:按住“Shift”的同时单击“刷新”,或按“Ctrl-F5”或“Ctrl-R”(Mac为“⌘-R”)
  • Google Chrome:按“Ctrl-Shift-R”(Mac为“⌘-Shift-R”)
  • Internet Explorer:按住“Ctrl”的同时单击“刷新”,或按“Ctrl-F5”
  • Opera:在“工具→首选项”中清除缓存
如果您已登录但该页面出现未登录状态,请尝试在地址栏的地址最后添加代码?_=1来访问最新页面。
添加代码后的本页地址如下:-{R|https://moegirl.icu/User:%E6%9C%BA%E6%99%BA%E7%9A%84%E5%B0%8F%E9%B1%BC%E5%90%9B/gadget/index.js?_=1}-
!(async function () {
  // Load utils
  await mw.loader.using(['mediawiki.api', 'mediawiki.util'])
  const config = mw.config.get()
  const { getUrl } = mw.util

  // Base page (User:<wgUserName>/gadget/)
  const BASE_PAGE = `User:${config.wgUserName}/gadget/`
  // Index page (index.json)
  const INDEX_PAGE = `${BASE_PAGE}index.json`
  // Get index
  const GADGETS_LIST = await $.ajax({
    url: getUrl(INDEX_PAGE, { action: 'raw', type: 'application/json' }),
    dataType: 'json',
    cache: true,
  })

  const getPage = (name) =>
    name.startsWith('/') ? name.slice(1) : `${BASE_PAGE}${name}`

  function loadScript(name) {
    const link = getUrl(getPage(name), {
      action: 'raw',
      ctype: 'text/javascript',
    })
    return mw.loader.load(link)
  }
  function loadStyle(name) {
    const link = getUrl(getPage(name), {
      action: 'raw',
      ctype: 'text/css',
    })
    return mw.loader.load(link, 'text/css')
  }

  $.each(GADGETS_LIST, function (_, item) {
    console.log('Load personal gadget', item)
    var { name, script, style } = item
    // Resolve scripts
    if (script === true && name) {
      script = name + '.js'
    }
    if (typeof script === 'string') script = [script]
    // Resolve styles
    if (style === true && name) {
      style = name + '.css'
    }
    if (typeof style === 'string') style = [style]
    // Fly
    $.each(script, function (_, name) {
      loadScript(name)
    })
    $.each(style, function (_, name) {
      loadStyle(name)
    })
  })
})()