Created
March 7, 2019 09:30
-
-
Save TooBug/a3d0f6c2cdd03c6b43067176e9c6febb to your computer and use it in GitHub Desktop.
猫眼电影票房数字识别替换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let canvas, ctx; | |
function init(){ | |
console.log('==== 初始化canvas 开始 ===='); | |
canvas = document.createElement('canvas'); | |
canvas.width = 100; | |
canvas.height = 100; | |
canvas.style.width = '100px'; | |
canvas.style.height = '100px'; | |
canvas.style.position = 'absolute'; | |
canvas.style.right = '0'; | |
canvas.style.bottom = '0'; | |
canvas.style.zIndex = '100000000'; | |
document.body.appendChild(canvas); | |
ctx = canvas.getContext('2d'); | |
ctx.textBaseline = 'top'; | |
console.log('==== 初始化canvas 结束 ===='); | |
} | |
function destroy(){ | |
ctx = null; | |
document.body.removeChild(canvas); | |
} | |
function draw(font, text){ | |
ctx.font = font; | |
ctx.fillText(text, 10, 10); | |
} | |
function clear(){ | |
ctx.clearRect(0, 0, 100, 100); | |
} | |
function getImageData(){ | |
let data = ctx.getImageData(0, 0, 100, 100); | |
let compressData = []; | |
debugger; | |
for(let i=0; i<data.data.length; i+=4){ | |
if(data.data[i + 0] || data.data[i + 1] || data.data[i + 2] || data.data[i + 3]){ | |
compressData[i/4] = 1; | |
}else{ | |
compressData[i/4] = 0; | |
} | |
} | |
return compressData; | |
} | |
var numCharCodeMap = { | |
}; | |
function guess(){ | |
console.log('==== 识别页面中的数字 开始 ===='); | |
init(); | |
let allCs = document.querySelectorAll('.cs'); | |
allCs.forEach((cs) => { | |
let text = cs.innerText; | |
for(let i=0; i<text.length; i++){ | |
let char = text[i]; | |
let code = char.charCodeAt(0); | |
// 非数字 | |
if(code < 50000) continue; | |
if(typeof numCharCodeMap[code] !== 'undefined') continue; | |
let number = guessNumber(code); | |
numCharCodeMap[code] = number; | |
console.log(`Unicode: ${code}, 识别结果: ${number}`); | |
} | |
}); | |
console.log(numCharCodeMap); | |
destroy(); | |
console.log('==== 识别页面中的数字 结束 ===='); | |
} | |
function guessNumber(code){ | |
clear(); | |
draw('80px cs', String.fromCharCode(code)); | |
let data = getImageData(); | |
let max = 0; | |
let target; | |
for(let i=0; i<10; i++){ | |
clear(); | |
ctx.fillStyle = '#ff0000'; | |
draw('72px helvetica', i + ''); | |
let guessData = getImageData(); | |
let same = 0; | |
for(let i=0; i<data.length; i++){ | |
if(data[i] === guessData[i]){ | |
same++ | |
} | |
} | |
if(same > max){ | |
max = same; | |
target = i; | |
} | |
} | |
clear(); | |
return target; | |
} | |
function dealPage(){ | |
console.log('==== 替换页面中的数字 开始 ===='); | |
document.querySelectorAll('.cs').forEach((element) => { | |
element.classList.remove('cs'); | |
let text = element.innerText; | |
for(let code in numCharCodeMap){ | |
let number = numCharCodeMap[code]; | |
text = text.replace(new RegExp(String.fromCharCode(code), 'g'), number); | |
} | |
element.innerText = text; | |
}); | |
console.log('==== 替换页面中的数字 结束 ===='); | |
} | |
function output(){ | |
console.log('结果如下:\n\n\n\n'); | |
console.log(document.querySelector('#ticket_content').innerText); | |
} | |
guess(); | |
dealPage(); | |
output(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment