added multilanguage support

This commit is contained in:
2022-06-10 21:49:13 -05:00
parent 5c29ba1ef2
commit 85ff5851b5
4 changed files with 77 additions and 54 deletions

View File

@@ -1,18 +1,33 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$schema": "http://json-schema.org/draft-04/schema",
"$id": "https://git.zomo.dev/zomo/browser-scripts-builder/raw/branch/main/meta.schema.json",
"title": "UserScriptMeta",
"description": "UserScript metadata fields, from https://violentmonkey.github.io/api/metadata-block/",
"type": "object",
"properties": {
"$schema": {
"type": "string",
"description": "Read https://violentmonkey.github.io/api/metadata-block/ for more examples and information about metadata"
},
"name": {
"description": "Script Name",
"description": "The name of the script, shown in script list and menus",
"type": ["string", "object"],
"properties": {
"default": {
"type": "string"
}
},
"additionalProperties": {
"type": "string"
},
"required": ["default"]
},
"namespace": {
"description": "Author namespace",
"description": "The combination of namespace and name is the unique identifier for a userscript",
"type": "string"
},
"match": {
"description": "",
"description": "Define rules to decide whether a script should be executed",
"type": ["string", "array"],
"minItems": 1,
"uniqueItems": true,
@@ -21,7 +36,7 @@
}
},
"excludematch": {
"description": "",
"description": "Define rules to decide whether a script should be executed",
"type": ["string", "array"],
"minItems": 1,
"uniqueItems": true,
@@ -30,19 +45,28 @@
}
},
"version": {
"description": "",
"description": "Version of the script, it can be used to check if a script has new versions",
"type": "string"
},
"description": {
"description": "",
"description": "A brief summary to describe the script",
"type": ["string", "object"],
"properties": {
"default": {
"type": "string"
}
},
"additionalProperties": {
"type": "string"
},
"required": ["default"]
},
"icon": {
"description": "",
"description": "Specify an icon for the script",
"type": "string"
},
"require": {
"description": "",
"description": "Require another script to execute before the current one\n\nThe value is the URL to the required script, which may be relative to the URL the script is being installed from",
"type": ["string", "array"],
"minItems": 1,
"uniqueItems": true,
@@ -51,7 +75,7 @@
}
},
"resource": {
"description": "",
"description": "Static resources that can be accessed in the script by GM_getResourceText and GM_getResourceURL\n\nThe value is composed of two parts, joined with one or more white spaces\n\nThe first part is the name of the resource, no white space is allowed in it\n\nThe second part is the URL to the resource, which may be relative to the URL the script is being installed from",
"type": ["string", "array"],
"minItems": 1,
"uniqueItems": true,
@@ -60,16 +84,16 @@
}
},
"runat": {
"description": "",
"description": "Decide when the script will execute\n\ndocument-end: (default) The script executes when DOMContentLoaded is fired\n\ndocument-start: The script executes as soon as possible\n\ndocument-idle: The script executes after DOMContentLoaded is fired, ensuring other resources like images are loaded",
"type": "string",
"enum": ["document-start", "document-end", "document-idle"]
"enum": ["document-end", "document-idle", "document-start"]
},
"noframes": {
"description": "",
"description": "When present, the script will execute only in top level document, but not in nested frames",
"type": "boolean"
},
"grant": {
"description": "",
"description": "Specify which special APIs should be granted and can be used when the script executes\n\nIf nothing is present, \"none\" is assumed\n\nAny GM_ or GM. function used, including window.close and window.focus",
"type": ["string", "array"],
"minItems": 1,
"uniqueItems": true,
@@ -78,26 +102,27 @@
}
},
"injectinto": {
"description": "",
"description": "Decide which context the script will be injected into\n\npage: Inject into context of the web page\nIn this mode, unsafeWindow refers to the window object, allowing the script to access JavaScript objects of the web page, just like normal page scripts can\n\ncontent: Inject into context of content scripts.\nIn this mode, unsafeWindow refers to the global object in content script. As a result, the script can access and modify the page's DOM, but cannot access JavaScript objects of the web page.\n\nauto: Try to inject into context of the web page. If blocked by CSP rules, inject as a content script.",
"type": "string",
"enum": ["page", "content", "auto"]
},
"downloadURL": {
"description": "",
"description": "The URL the script can be downloaded from",
"type": "string"
},
"supportURL": {
"description": "",
"description": "If supplied, the question mark icon in the user scripts list will link to this.",
"type": "string"
},
"homepageURL": {
"description": "",
"description": "If supplied, the home icon in the user scripts list will link to this.",
"type": "string"
},
"unwrap": {
"description": "",
"description": "If supplied, the script will be injected as is into the global scope of the page, i.e. without our standard wrapper like window.VMxxx=function(){...}.\n\nThe @grant key is ignored so the script won't have access to GM.* or GM_* API. ",
"type": "boolean"
}
},
"additionalProperties": false,
"required": ["name", "namespace", "version"]
}

View File

@@ -30,22 +30,6 @@ export default async function (
bundle: true,
minify: false,
// write: false, //TODO this will cause result.outputFiles to have the file contents so i can write the file instead
// watch: !watchCallback
// ? false
// : {
// onRebuild(err, _result) {
// console.log('onrebuild')
// let error = null
// if (err) {
// console.error(name, err)
// error = (err as BuildFailure).message
// }
// watchCallback(metaJson, error)
// },
// },
define: {
UserScriptName: `'${metaJson.name}'`,
UserScriptNamespace: `'${metaJson.namespace}'`,

View File

@@ -79,12 +79,20 @@ ${(Object.keys(meta) as Array<keyof UserScriptMetaFull>)
: key
key_str = key_str.padEnd(12, ' ')
if (typeof val === 'boolean') {
if (typeof val === 'boolean') { //bool
if (val) return `// @${key_str}`
} else if (typeof val === 'string') {
} else if (typeof val === 'string') { //string
return `// @${key_str} ${val}`
} else if (Array.isArray(val)) {
} else if (Array.isArray(val)) { //multiple
return val.map(v => `// @${key_str} ${v}`).join('\n')
} else if (typeof val === 'object') { //multilingual
let langs = val
return Object.keys(langs)
.map(lang => {
let langStr = lang === 'default' ? '' : `:${lang}`
return `// @${key_str}${langStr} ${langs[lang]}`
})
.join('\n')
}
return ''

View File

@@ -1,16 +1,22 @@
export type UserScriptMetaMultilingual = string | {
default: string
[lang: string]: string
}
export type UserScriptMetaMultiple = string | string[]
export interface UserScriptMetaPartial {
name?: string
name?: UserScriptMetaMultilingual
namespace?: string
match?: string | string[]
excludematch?: string | string[]
match?: UserScriptMetaMultiple
excludematch?: UserScriptMetaMultiple
version?: string
description?: string
description?: UserScriptMetaMultilingual
icon?: string
require?: string | string[]
resource?: string | string[]
require?: UserScriptMetaMultiple
resource?: UserScriptMetaMultiple
runat?: 'document-start' | 'document-end' | 'document-idle' | ''
noframes?: boolean
grant?: string | string[]
grant?: UserScriptMetaMultiple
injectinto?: 'page' | 'content' | 'auto' | ''
downloadURL?: string
supportURL?: string
@@ -19,7 +25,7 @@ export interface UserScriptMetaPartial {
}
export interface UserScriptMeta extends UserScriptMetaPartial {
name: string
name: UserScriptMetaMultilingual
namespace: string
version: string
downloadURL: string
@@ -28,15 +34,15 @@ export interface UserScriptMeta extends UserScriptMetaPartial {
}
export interface UserScriptMetaFull extends UserScriptMeta {
match: string | string[]
excludematch: string | string[]
description: string
match: UserScriptMetaMultiple
excludematch: UserScriptMetaMultiple
description: UserScriptMetaMultilingual
icon: string
require: string | string[]
resource: string | string[]
require: UserScriptMetaMultiple
resource: UserScriptMetaMultiple
runat: 'document-start' | 'document-end' | 'document-idle' | ''
noframes: boolean
grant: string | string[]
grant: UserScriptMetaMultiple
injectinto: 'page' | 'content' | 'auto' | ''
unwrap: boolean
}