diff options
Diffstat (limited to 'captcha-parent.js')
-rw-r--r-- | captcha-parent.js | 101 |
1 files changed, 68 insertions, 33 deletions
diff --git a/captcha-parent.js b/captcha-parent.js index eaef67b..86ab909 100644 --- a/captcha-parent.js +++ b/captcha-parent.js @@ -73,6 +73,18 @@ HCHA._get_rc_version = async function() { return match[1]; } +HCHA._make_default_site_url = function() { + const url_obj = new URL(window.location.href); + let port = url_obj.port; + + if (url_obj.protocol === "http:" && port === "80") + port = ""; + else if (url_obj.protocol === "https:" && port === "") + port = "443"; + + return url_obj.origin + (port && `:${port}`); +} + HCHA._make_anchor_frame_url = function(site_key, site_url, rc_version) { const url = new URL("https://google.com/recaptcha/api2/anchor"); url.search = new URLSearchParams({ @@ -107,56 +119,79 @@ HCHA._spawn_frame = function(container, site_key, site_url, rc_version) { HCHA._make_anchor_frame_url(site_key, site_url, rc_version); } -/* This library's external API consists of the run() function defined below. */ -HCHA.run = async function(container, site_key, site_url, - start_immediately=false) { - site_url ||= window.location.origin; +HCHA._show_msg_and_wait = async function(container, button_text, + paragraph_text=null) { + container.innerHTML = "<div><p></p><button></button></div>"; - const rc_version = await HCHA._get_rc_version(); + HCHA._style_contained_element(container.firstElementChild); - if (!start_immediately) { - container.innerHTML = - "<div><button>Start solving reCAPTCHA</button></div>"; + const paragraph = container.querySelector("p"); - HCHA._style_contained_element(container.firstElementChild); + if (paragraph_text === null) + paragraph.remove(); + else + paragraph.textContent = paragraph_text; - const button = container.querySelector("button"); - await new Promise(cb => button.onclick = e => { - e.preventDefault(); - cb(); - }); - } + const button = container.querySelector("button"); + + button.textContent = button_text; + + await new Promise(cb => button.onclick = e => { + e.preventDefault(); + cb(); + }); +} - HCHA._spawn_frame(container, site_key, site_url, rc_version); +HCHA._show_success_info = async function(container) { + const msg = "CAPTCHA completed successfully. The token shall be valid for 2 minutes."; + await HCHA._show_msg_and_wait(container, "OK", msg); - let event, resolve; + container.innerHTML = ""; +} + +HCHA._main_loop = async function*(container, site_key, site_url) { + const rc_version = await HCHA._get_rc_version(); while (true) { + HCHA._spawn_frame(container, site_key, site_url, rc_version); + + let event, resolve; + const prom = new Promise(cb => resolve = cb); window.addEventListener("message", resolve, {once: true}); event = await prom; - if (event.origin !== "https://google.com") + if (event.origin !== "https://google.com") { console.warn("Received message from unexepected origin!", event); - else if (event.data === "recreate_frame") + } else if (event.data === "recreate_frame") { HCHA._spawn_frame(container, site_key, site_url, rc_version); - else if (typeof event.data !== "string") + } else if (typeof event.data !== "string") { console.error("Received token is not a string!", event); - else - break; + } else { + HCHA._show_success_info(container); + + yield event.data; + + await new Promise(cb => setTimeout(cb, 120000)); + + yield null; + + await HCHA._show_msg_and_wait(container, "Solve reCAPTCHA again", + "Reached token timeout."); + } } +} + +/* This library's external API consists of the run() function defined below. */ +HCHA.run = async function*(container, site_key, site_url, + start_immediately=false) { + site_url = site_url || HCHA._make_default_site_url(); + + if (!start_immediately) + await HCHA._show_msg_and_wait(container, "Start solving reCAPTCHA"); - container.innerHTML = ` -<div style="width: 302px; border-style: none; padding: 10px"> - <p> - CAPTCHA completed successfully. The token shall be valid for 2 minutes. - </p> - <button>OK</button> -</div> -` - container.querySelector("button").onclick = () => container.innerHTML = ""; - - return event.data; + for await (const value of HCHA._main_loop(container, site_key, site_url)) + yield value; } |