Skip to content

Commit

Permalink
popup mods, auth header
Browse files Browse the repository at this point in the history
  • Loading branch information
j-w-yun committed Dec 1, 2022
1 parent 1b26b94 commit aa9f7e7
Show file tree
Hide file tree
Showing 11 changed files with 388 additions and 157 deletions.
110 changes: 76 additions & 34 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,38 +123,66 @@ class Settings {
static data = {};

static _save() {
return new Promise(resolve => bapi.browser.storage.sync.set({settings: Settings.data}, resolve));
console.log('Settings.save', Settings.data);
return new Promise(resolve => {
bapi.browser.storage.sync.set({settings: Settings.data}, resolve);
});
}

static load() {
// static load() {
// return new Promise(resolve => {
// const storage = bapi.browser.storage;
// if (!storage) {
// resolve();
// return;
// }
// storage.sync.get(['settings'], async ({settings}) => {
// if (!settings) {
// await Settings.reset();
// }
// else {
// Settings.data = settings;
// if (Settings.data.version !== SettingsManager.DEFAULT.version) {
// const key = Settings.data.key;
// console.log('mismatched version', key);
// await Settings.reset();
// Settings.data.key = key;
// }
// }
// resolve();
// });
// });
// }

static _get_settings() {
return new Promise(resolve => {
const storage = bapi.browser.storage;
if (!storage) {
resolve();
return;
}
storage.sync.get(['settings'], async ({settings}) => {
if (!settings) {
await Settings.reset();
}
else {
Settings.data = settings;
if (Settings.data.version !== SettingsManager.DEFAULT.version) {
const key = Settings.data.key;
console.log('mismatched version', key);
await Settings.reset();
Settings.data.key = key;
}
}
// // Temporary fix
// if (Settings.data.key?.startsWith('MIIBI')) {
// Settings.data.key = '';
// }
resolve();
bapi.browser.storage.sync.get(['settings'], ({settings}) => {
console.log('Settings._get_settings', settings);
resolve(settings);
});
});
}

static async load() {
for (let i = 0; i < 4; i++) {
const settings = await Settings._get_settings();
if (!settings) {
// await Time.sleep(100);
continue;
}
Settings.data = settings;
if (Settings.data.version !== SettingsManager.DEFAULT.version) {
const key = Settings.data.key;
console.log('mismatched version', key);
await Settings.reset();
Settings.data.key = key;
}
return;
}
console.log('settings load retry exceeded');
await Settings.reset();
}

static async get() {
return Settings.data;
}
Expand All @@ -164,21 +192,35 @@ class Settings {
await Settings._save();
}

static async update({settings}) {
for (const [k, v] of Object.entries(settings)) {
Settings.data[k] = v;
}
await Settings._save();
}

static async replace({settings}) {
Settings.data = settings;
await Settings._save();
}

static async reset() {
Settings.data = deep_copy(SettingsManager.DEFAULT);

// Set key from manifest
const manifest = bapi.browser.runtime.getManifest();
if (manifest.nopecha_key) {
Settings.data.key = manifest.nopecha_key;
}
// // Set key from manifest
// const manifest = bapi.browser.runtime.getManifest();
// if (manifest.nopecha_key) {
// Settings.data.key = manifest.nopecha_key;
// }

await Settings._save();
}

static {
API.register(this, 'get');
API.register(this, 'set');
API.register(this, 'update');
API.register(this, 'replace');
API.register(this, 'reset');
}
}
Expand Down Expand Up @@ -357,7 +399,7 @@ class Server {
if (key === 'undefined') {
key = '';
}
const r = await fetch(`${Server.ENDPOINT}&k=${key}`);
const r = await fetch(`${Server.ENDPOINT}&key=${key}`);
plan = JSON.parse(await r.text());
} catch {}
Server.is_fetching_plan = false;
Expand Down Expand Up @@ -406,7 +448,7 @@ class Relay {


class Icon {
static set_icon({status}) {
static set({status}) {
return new Promise(resolve => {
const ba = bapi.VERSION === 'firefox' ? bapi.browser.browserAction : bapi.browser.action;
if (status === 'on') {
Expand Down Expand Up @@ -474,7 +516,7 @@ class Icon {
}

static {
API.register(this, 'set_icon');
API.register(this, 'set');
}
}

Expand Down Expand Up @@ -538,7 +580,7 @@ class ContextMenu {

await Settings.load();

await Icon.set_icon({status: Settings.data.enabled ? 'on' : 'off'});
await Icon.set({status: Settings.data.enabled ? 'on' : 'off'});

bapi.browser.runtime.onMessage.addListener((req, sender, send) => {
const method = req[0];
Expand Down
27 changes: 20 additions & 7 deletions content.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class BG {
// static exec(method, data) {
static exec() {
return new Promise(resolve => {
try {
Expand Down Expand Up @@ -114,8 +113,8 @@ class NopeCHA {
const data = {
type: captcha_type,
task: task,
v: chrome.runtime.getManifest().version,
key: key,
v: chrome.runtime.getManifest().version,
url: info ? info.url : window.location.href,
};
if (image_urls) {
Expand All @@ -131,9 +130,17 @@ class NopeCHA {
data.audio_data = audio_data;
}

const text = await Net.fetch(NopeCHA.INFERENCE_URL, {method: 'POST', body: JSON.stringify(data), headers: {'Content-Type': 'application/json'}});

try {
const headers = {'Content-Type': 'application/json'};
if (key && key !== 'undefined') {
headers.Authorization = `Bearer ${key}`;
}
const text = await Net.fetch(NopeCHA.INFERENCE_URL, {
method: 'POST',
headers: headers,
body: JSON.stringify(data),
});

const r = JSON.parse(text);
if ('error' in r) {
if (r.error === NopeCHA.ERRORS.RATE_LIMITED) {
Expand All @@ -158,14 +165,14 @@ class NopeCHA {
return await NopeCHA.get({job_id, key});
} catch (e) {
console.log('failed to parse post response', e);
break;
// break;
}
}

return {job_id: null, data: null};
}

static async get({key, job_id}) {
static async get({job_id, key}) {
const start_time = Date.now();

while (true) {
Expand All @@ -175,7 +182,13 @@ class NopeCHA {
}

await Time.sleep(1000);
const text = await Net.fetch(`${NopeCHA.INFERENCE_URL}?id=${job_id}&key=${key}`);
const headers = {};
if (key && key !== 'undefined') {
headers.Authorization = `Bearer ${key}`;
}
const text = await Net.fetch(`${NopeCHA.INFERENCE_URL}?id=${job_id}&key=${key}`, {
headers: headers,
});
try {
const r = JSON.parse(text);
if ('error' in r) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.base.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "NopeCHA: CAPTCHA Solver",
"version": "0.3.2",
"version": "0.3.3",
"description": "Automatically solve reCAPTCHA, hCaptcha, FunCAPTCHA, AWS WAF, and text CAPTCHA using AI.",
"permissions": [
"storage",
Expand Down
59 changes: 48 additions & 11 deletions popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ html, body {
div {
display: block;
}
a {
text-decoration: none;
}
button, input, optgroup, select, textarea {
font-family: inherit;
font-size: 100%;
Expand Down Expand Up @@ -166,6 +169,14 @@ input[type=number] {
100% {opacity: 1;}
}

/* MISC */
.clickable {
cursor: pointer !important;
}
.clickable:hover {
opacity: 0.8 !important;
}

/* APP */

#app_frame {
Expand Down Expand Up @@ -261,9 +272,6 @@ input[type=number] {
margin-left: auto;
display: flex;
}
.plan_value > a {
text-decoration: none;
}
.plan_button {
display: flex;
background-color: transparent;
Expand All @@ -281,13 +289,7 @@ input[type=number] {
color: #0a95ff;
}
.plan_button.link,
.plan_button.clickable {
cursor: pointer;
}
.plan_button.link:hover,
.plan_button.clickable:hover {
opacity: 0.8;
}
.plan_button_label {
box-sizing: border-box;
font-size: 14px;
Expand Down Expand Up @@ -319,14 +321,34 @@ input[type=number] {

/* KEY */

.key_label {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
width: 100%;
}
.key_label > .instructions {
font-weight: normal;
line-height: 16px;
margin-left: 6px;
color: #fff;
font-size: 10px;
}
.settings_text[data-settings="key"] {
background: #1a2432;
position: absolute;
width: calc(100% - 32px);
transition: all ease 0.1s;
z-index: 1;
}
#edit_key {
/* .edit_key {
line-height: 16px;
margin-right: 6px;
color: #fff;
font-size: 10px;
} */
.edit_icon {
z-index: 2;
}

Expand Down Expand Up @@ -382,6 +404,16 @@ button.menu_item_container {
width: 16px;
}

/* #export {
color: rgba(255, 255, 255, 0.5);
font-size: 1.2em;
cursor: pointer;
transition: color 0.5s ease 0s, border 0.5s ease 0s;
}
#export:hover {
color: rgb(255, 255, 255);
} */

/* TAB */

.bbflex {
Expand All @@ -399,11 +431,16 @@ button.menu_item_container {
}
.settings_item_container {
box-sizing: border-box;
margin-bottom: 16px;
margin-bottom: 8px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
box-sizing: border-box;
}
.settings_item_container > a {
color: rgba(255, 255, 255, 0.5);
text-decoration: none;
transition: color 0.5s ease 0s, border 0.5s ease 0s;
}
.settings_item {
width: 100%;
background-color: rgba(255, 255, 255, 0.08);
Expand Down
Loading

0 comments on commit aa9f7e7

Please sign in to comment.