利用chrome调用本地程序破解图片验证码
background.js
var port = null, tabId, fname = "vcode.chrome.v1.item.01"; //对应注册表HKEY_CURRENT_USERSoftwareGoogleChromeNativeMessagingHosts var hostname = 'com.dbing.net.host'; //onNativeDisconnect function onDisconnected() { port = null; } // function connect(data) { port = chrome.runtime.connectNative(hostname); port.onDisconnect.addListener(onDisconnected); port.onMessage.addListener(onNativeMessage); port.postMessage(data); } function onNativeMessage(msg) { chrome.tabs.sendMessage(tabId, { cmd: 'setCode', code:msg }); }// 监听消息 chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { tabId= sender.tab['id']; if (!request.cmd) return; if (request.cmd=='getCode') connect(request.url); });
test.js
var nodeCode = document.getElementsByClassName('login-code'); var nodeImg, timer; send(); function send() { if (nodeCode && nodeCode[0] && nodeCode[0].nextElementSibling) { nodeCode[0].children[0].innerText = ''; nodeImg = nodeCode[0].nextElementSibling; nodeImg.onload = function () { sendMsg(); } } }; function sendMsg() { var r = nodeImg.getBoundingClientRect(); var rect = { x: r.x, y: r.y + window.outerHeight - window.innerHeight, r.width, height: r.height } var rs = Math.round(rect.x) + ',' + Math.round(rect.y) + ',' + Math.round(rect.width) + ',' + Math.round(rect.height); chrome.runtime.sendMessage({ cmd: 'getCode', url: rs }); } // 监听消息 chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) { if (request.cmd == 'setCode') { console.log('setCode,login-code.length:', nodeCode.length) if (!nodeCode) return; nodeCode[0].children[1].value = request.code; sendResponse({ status: 1 }); } });
app.json
{ "name": "com.dbing.net.host", //名称需跟注册表的项名称一致 "description": "open host pro", "path": "C:\Users\Administrator\Pro\Demo\Pro.AI\bin\Debug\Pro.AI.exe", "type": "stdio", "allowed_origins": [ "chrome-extension://blejflebmbmpebolcigedgkclijabeop/" ] }
manifest.json
{ "name": "本地管道通信", //名称 "description": "chrome.本地通讯.管道流", //介绍 "version": "1.0.1", //版本 "manifest_version": 2, //必须为2 "permissions": [ //必须,定义权限,需要和本地应用程序通信 "nativeMessaging", "downloads" ], "content_scripts": [ { "matches": [ "*://*.google.com/*" ], "js": [ "test.js" ], "run_at": "document_end" } ], "background": { "scripts": [ "background.js" ] } }