Skip to content

Instantly share code, notes, and snippets.

@hjkcai
Created June 18, 2020 06:06
Show Gist options
  • Save hjkcai/a3b1c1a79ccfa7c108580b1a15059ae0 to your computer and use it in GitHub Desktop.
Save hjkcai/a3b1c1a79ccfa7c108580b1a15059ae0 to your computer and use it in GitHub Desktop.
// 触宝输入法快捷输入配置文件制作器
// 文件位置: /data/data/com.cootek.smartinputv5/files/v5701/shortcut.lst
const fs = require('fs')
function read() {
const data = fs.readFileSync('/tmp/shortcut.lst')
let offset = 4
const count = data.readUInt32LE()
const result = Array.from({ length: count }, () => {
const shortcutLength = data.readUInt32LE(offset);
offset += 4
const shortcut = data.slice(offset, offset + shortcutLength * 2).toString('ucs-2')
offset += shortcutLength * 2
const expandationLength = data.readUInt32LE(offset);
offset += 4
const expandation = data.slice(offset, offset + expandationLength * 2).toString('ucs-2')
offset += expandationLength * 2
return { shortcut, expandation }
})
console.log(JSON.stringify(result, null, 2))
}
function write() {
const data = JSON.parse(fs.readFileSync('/tmp/shortcut.json', 'utf8'))
const dist = '/tmp/shortcut.lst'
const list = data.map(({ shortcut, expandation }) => {
const shortcutBuf = Buffer.from(shortcut, 'ucs-2')
const shortcutLength = Buffer.alloc(4)
shortcutLength.writeUInt32LE(shortcutBuf.length / 2)
const expandationBuf = Buffer.from(expandation, 'ucs-2')
const expandationLength = Buffer.alloc(4)
expandationLength.writeUInt32LE(expandationBuf.length / 2)
return Buffer.concat([shortcutLength, shortcutBuf, expandationLength, expandationBuf])
})
const length = Buffer.alloc(4)
length.writeUInt32LE(list.length)
fs.writeFileSync(dist, Buffer.concat([length, ...list]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment