Tampermonkey Meta

油猴头部信息解析工具

Ten skrypt nie powinien być instalowany bezpośrednio. Jest to biblioteka dla innych skyptów do włączenia dyrektywą meta // @require https://update.greasyfork.org/scripts/440136/1019188/Tampermonkey%20Meta.js

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

/**
 * 获取油猴头设置:  @设置名=设置值
 * 
 * @exmaple
 * 
 * 
 * 
 * 
 * 

```js

// ==UserScript==
// @number       1
// @string       this is string
// @boolean      true
// @object.somekey.key1  1
// @object.somekey.key2  this is object      
// ==/UserScript==


const metas = GM_meta(GM_info.scriptMetaStr," ")

console.log(metas);
// {number:1, string:"this is string", boolean: true, object:{someKey:{key1:1,key2:"this is object"}}}  

```


 
 * @params metaString       油猴头,通常使用 GM_info.scriptMetaStr 获取
 * @params separator        分隔符,默认一个空格 : ' '
 */
function GM_meta(metaString,separator = " "){
    const object = {}
    const regexp = RegExp(`// *@(.*?)${separator}(.*)`)


    let data = metaString.match(RegExp(regexp,"g"))
    // 去除空格
    data
        .map(item=>item.trim())
    // 转换成 key value 形式
        .map(item=>{
        const match = item.match(regexp)
        return { key: match[1].trim(), value: match[2].trim() }
    })
    // 类型转换
        .map(item=>{
        item.value =
            Number(item.value) ? Number(item.value)
        :item.value === '开启' ? true
        :item.value === '关闭' ? false
        :String(item.value)
        return item
    })
    // 生成对象
        .forEach(item=>{
        // 使用 `.` 进行深度对象创建
        if(item.key.includes('.')){
            const keys = item.key.split(".")
            const endKey = keys.pop()
            let obj = undefined
            for(const key of keys){
                let target = obj || object
                if(target[key] === undefined){
                    target[key] = {}
                }
                obj = target[key]
            }
            obj[endKey] = item.value
        }else{
            object[item.key] = item.value
        }


    })

    return object
}