From 7108f4179cf29d713db5bb5da9cd16e42948a51e Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 25 Jan 2024 10:18:17 +0800 Subject: [PATCH] update chrome extension scripts --- chrome_tixcraft.py | 2 +- config_launcher.py | 2 +- settings.py | 2 +- webdriver/Maxbotplus_1.0.0/background.js | 6 + .../Maxbotplus_1.0.0/js/ticket_ticket.js | 152 ++++++++++++++++++ webdriver/Maxbotplus_1.0.0/manifest.json | 16 +- 6 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 webdriver/Maxbotplus_1.0.0/js/ticket_ticket.js diff --git a/chrome_tixcraft.py b/chrome_tixcraft.py index 34f5b6e..c2dac43 100644 --- a/chrome_tixcraft.py +++ b/chrome_tixcraft.py @@ -41,7 +41,7 @@ try: except Exception as exc: pass -CONST_APP_VERSION = "MaxBot (2024.01.15)" +CONST_APP_VERSION = "MaxBot (2024.01.16)" CONST_MAXBOT_ANSWER_ONLINE_FILE = "MAXBOT_ONLINE_ANSWER.txt" CONST_MAXBOT_CONFIG_FILE = "settings.json" diff --git a/config_launcher.py b/config_launcher.py index f7117f9..cbb8070 100644 --- a/config_launcher.py +++ b/config_launcher.py @@ -22,7 +22,7 @@ import sys import threading import webbrowser -CONST_APP_VERSION = "MaxBot (2024.01.15)" +CONST_APP_VERSION = "MaxBot (2024.01.16)" CONST_MAXBOT_LAUNCHER_FILE = "config_launcher.json" CONST_MAXBOT_CONFIG_FILE = "settings.json" diff --git a/settings.py b/settings.py index 2b60574..92690dc 100644 --- a/settings.py +++ b/settings.py @@ -40,7 +40,7 @@ try: except Exception as exc: pass -CONST_APP_VERSION = "MaxBot (2024.01.15)" +CONST_APP_VERSION = "MaxBot (2024.01.16)" CONST_MAXBOT_ANSWER_ONLINE_FILE = "MAXBOT_ONLINE_ANSWER.txt" CONST_MAXBOT_CONFIG_FILE = "settings.json" diff --git a/webdriver/Maxbotplus_1.0.0/background.js b/webdriver/Maxbotplus_1.0.0/background.js index 1fa6c23..729197a 100644 --- a/webdriver/Maxbotplus_1.0.0/background.js +++ b/webdriver/Maxbotplus_1.0.0/background.js @@ -12,6 +12,12 @@ chrome.runtime.onInstalled.addListener(function(){ text: default_status }); + chrome.storage.local.set( + { + status: default_status + } + ); + const default_webserver_runing=false; fetch("data/settings.json") diff --git a/webdriver/Maxbotplus_1.0.0/js/ticket_ticket.js b/webdriver/Maxbotplus_1.0.0/js/ticket_ticket.js new file mode 100644 index 0000000..c16c198 --- /dev/null +++ b/webdriver/Maxbotplus_1.0.0/js/ticket_ticket.js @@ -0,0 +1,152 @@ +const storage = chrome.storage.local; +var settings = null; + +$("footer").remove(); + +function ticket_assign_adjacent_seat(flag) { + //console.log("disable_adjacent_seat flag:"+flag); + if(flag) { + $('input[type=checkbox]').each(function() { + $(this).prop('checked', true); + }); + } +} + +function ticket_focus_on_captcha() +{ + $("div.login-form input[autocomplete='off']").focus(); +} + +var myInterval = null; + +function ticket_get_ocr_image() +{ + //console.log("get_ocr_image"); + let image_data = ""; + + // PS: tixcraft have different domain to use the same content script. + const currentUrl = window.location.href; + const domain = currentUrl.split('/')[2]; + + let image_id = 'chk_pic'; + let img = document.getElementById(image_id); + if(img!=null) { + let canvas = document.createElement('canvas'); + let context = canvas.getContext('2d'); + canvas.height = img.naturalHeight; + canvas.width = img.naturalWidth; + context.drawImage(img, 0, 0); + let img_data = canvas.toDataURL(); + if(img_data) { + image_data = img_data.split(",")[1]; + //console.log(image_data); + } + } + return image_data; +} + +chrome.runtime.onMessage.addListener((message) => { + //console.log('sent from background', message); + ticket_set_ocr_answer(message.answer); +}); + +function ticket_set_ocr_answer(answer) +{ + //console.log("answer:"+answer); + if(answer.length > 0) { + const currentUrl = window.location.href; + const domain = currentUrl.split('/')[2]; + const scrip_page = currentUrl.split('/')[5]; + + answer = answer.toUpperCase(); + let query_string = "div.form-group input[autocomplete='off']"; + if(domain.indexOf('kham') > -1) { + query_string = ".step2Login input[maxlength='4']"; + } + $(query_string).val(answer); + //console.log($(query_string).length); + //$("div#ticket-wrap a[onclick]").click(); + //$("#aspnetForm").submit(); + let done_div=""; + $("body").append(done_div); + + } +} + +async function ticket_get_ocr_answer(api_url, image_data) +{ + let bundle = { + action: 'ocr', + data: { + 'url': api_url + 'ocr', + 'image_data':image_data, + } + }; + + let bundle_string = JSON.stringify(bundle); + const return_answer = await chrome.runtime.sendMessage(bundle); + //console.log(return_answer); +} + +function ticket_orc_image_ready(api_url) +{ + let ret=false; + let image_data = ticket_get_ocr_image(); + if(image_data.length>0) { + ret=true; + if(myInterval) clearInterval(myInterval); + ticket_get_ocr_answer(api_url, image_data); + } + //console.log("ticket_orc_image_ready:"+ret); + return ret; +} + +storage.get('settings', function (items) +{ + if (items.settings) + { + settings = items.settings; + } else { + console.log('no settings found'); + } +}); + + +storage.get('settings', function (items) +{ + if (items.settings) + { + settings = items.settings; + } +}); + +storage.get('status', function (items) +{ + if (items.status && items.status=='ON') + { + //console.log("ticket_number:"+ settings.ticket_number); + //ticket_assign_adjacent_seat(settings.advanced.disable_adjacent_seat); + + // ocr + if(settings.ocr_captcha.enable) { + let remote_url_string = ""; + let remote_url_array = []; + if(settings.advanced.remote_url.length > 0) { + remote_url_array = JSON.parse('[' + settings.advanced.remote_url +']'); + } + if(remote_url_array.length) { + remote_url_string = remote_url_array[0]; + } + if(!ticket_orc_image_ready(remote_url_string)) { + myInterval = setInterval(() => { + ticket_orc_image_ready(remote_url_string); + }, 100); + } + } else { + // no orc, just focus; + ticket_focus_on_captcha(); + } + } else { + console.log('no status found'); + } +}); diff --git a/webdriver/Maxbotplus_1.0.0/manifest.json b/webdriver/Maxbotplus_1.0.0/manifest.json index 315be7a..e4defee 100644 --- a/webdriver/Maxbotplus_1.0.0/manifest.json +++ b/webdriver/Maxbotplus_1.0.0/manifest.json @@ -163,6 +163,20 @@ "js/ticket_area.js" ] }, + { + "matches": [ + "https://ticket.com.tw/application/UTK02/UTK0205_.aspx?*", + "https://ticket.com.tw/application/UTK02/UTK0202_.aspx?*", + "https://kham.com.tw/application/UTK02/UTK0205_.aspx?*", + "https://kham.com.tw/application/UTK02/UTK0202_.aspx?*" + ], + "run_at": "document_end", + "js": [ + "jquery.min.js", + "js/common.js", + "js/ticket_ticket.js" + ] + }, { "matches": [ "https://kham.com.tw/application/utk01/UTK0101_03.aspx" @@ -366,5 +380,5 @@ ] } ], - "version": "1.0.6" + "version": "1.0.7" } \ No newline at end of file