跳转到内容

模組:KillRSP/Whitelist

维基百科,自由的百科全书
local p = {}

function p.getWhitelist(frame)
    local res = {
        domains = {},
        names = {}
    }

    local success, data = pcall(mw.loadData, 'Module:RSP/Whitelist')
    if success and type(data) == 'table' then
        for _, entry in pairs(data) do
            if type(entry) == 'table' then
                -- 提取名称
                if entry.name and entry.name ~= "" then
                    res.names[entry.name] = true
                end
                -- 提取域名
                if type(entry.domains) == 'table' then
                    for _, d in ipairs(entry.domains) do
                        if type(d) == 'string' and d ~= "" then
                            res.domains[d] = true
                        end
                    end
                end
            end
        end
    end

    -- 如果是由 #invoke 调用,返回 JSON 字符串供前端 JS 使用
    -- 如果是被其他 Lua 模块 require,则可以直接返回 table
    if frame == mw.getCurrentFrame() then
        return mw.text.jsonEncode(res)
    else
        return res
    end
end

return p