From 4edb97244bf73b53ee11db32be50e0bba8304cd9 Mon Sep 17 00:00:00 2001 From: Misaka <447f.misaka@outlook.com> Date: Sat, 21 Sep 2019 14:58:41 +0800 Subject: [PATCH] feat: encoder && decoder for twitter --- src/social-network-provider/twitter.com/index.ts | 13 ++++++++++++- src/utils/utils.ts | 8 ++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/social-network-provider/twitter.com/index.ts b/src/social-network-provider/twitter.com/index.ts index 1583760b6ded..27fb8c9d81d3 100644 --- a/src/social-network-provider/twitter.com/index.ts +++ b/src/social-network-provider/twitter.com/index.ts @@ -1,6 +1,7 @@ import { SocialNetworkWorkerAndUIDefinition } from '../../social-network/shared' import { usernameValidator } from './utils/user' -import { regexMatch } from '../../utils/utils' +import { batchReplace, regexMatch } from '../../utils/utils' +import { isNil } from 'lodash-es' export const host = 'twitter.com' export const hostURL = 'https://twitter.com' @@ -16,5 +17,15 @@ export const sharedSettings: SocialNetworkWorkerAndUIDefinition = { init() {}, publicKeyEncoder: (text: string) => `🎭${text}🎭`, publicKeyDecoder: (text: string) => regexMatch(text, /(🎭)(.+)(🎭)/, 2), + payloadEncoder: (text: string) => + `https://google.com/${batchReplace(text, [['🎼', '%20'], [':||', '%40'], ['+', '-'], ['=', '_'], ['|', '.']])}`, + payloadDecoder: (text: string) => { + let r = regexMatch(text, /https:\/\/google\.com\/%20(.+)%40/, 1) + if (isNil(r)) { + return 'null' + } + r = batchReplace(r, [['-', '+'], ['_', '='], ['.', '|']]) + return `🎼${r}:||` + }, notReadyForProduction: true, } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 55ad2c790bae..636064d73bb3 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -61,3 +61,11 @@ export const regexMatch = (str: string, regexp: RegExp, index: number) => { } export const isDocument = (node: Node): node is Document => node.nodeType === Node.DOCUMENT_NODE + +export const batchReplace = (source: string, group: Array<[string | RegExp, string]>) => { + let storage = source + for (const v of group) { + storage = storage.replace(v[0], v[1]) + } + return storage +}