Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Attempt to fix the problem of the extension working in the background #88

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,58 @@ chrome.runtime.onInstalled.addListener(async () => {
});

const kvStorage = {};
let fakeIdToId = {};
chrome.runtime.onMessage.addListener(function (
{ type, label },
sender,
sendResponse
) {
(async () => {
if (type === 'KV_SET') {
if (label.tab_specific) {
label.key = `${sender.tab.id}_${label.key}`;
}
kvStorage[label.key] = label.value;
sendResponse({ status: 'success' });
} else if (type === 'KV_GET') {
if (label.tab_specific) {
label.key = `${sender.tab.id}_${label.key}`;
}
sendResponse({ status: 'success', value: kvStorage[label.key] });
if (type === 'KV_SET') {
if (label.tab_specific) {
label.key = `${sender.tab.id}_${label.key}`;
}
kvStorage[label.key] = label.value;
sendResponse({ status: 'success' });
} else if (type === 'KV_GET') {
if (label.tab_specific) {
label.key = `${sender.tab.id}_${label.key}`;
}
sendResponse({ status: 'success', value: kvStorage[label.key] });
} else if (type === 'HackTimer') {
if (label.name === 'setInterval') {
fakeIdToId[label.fakeId] = setInterval(function () {
triggerTimer(label.name, sender, label.fakeId);
}, label.time);
} else if (label.name === 'clearInterval') {
clearInterval(fakeIdToId[label.fakeId]);
delete fakeIdToId[label.fakeId];
} else if (label.name === 'setTimeout') {
fakeIdToId[label.fakeId] = setTimeout(function () {
triggerTimer(label.name, sender, label.fakeId);
delete fakeIdToId[label.fakeId];
}, label.time);
} else if (label.name === 'clearTimeout') {
clearTimeout(fakeIdToId[label.fakeId]);
delete fakeIdToId[label.fakeId];
}
})();
return true;
}
});

async function triggerTimer(name, sender, fakeId) {
try {
await chrome.tabs.sendMessage(
sender.tab.id,
{
type: 'HackTimer',
label: { fakeId },
},
{
documentId: sender.documentId,
frameId: sender.frameId,
}
);
} catch (error) {
if (name === 'setInterval') clearInterval(fakeIdToId[fakeId]);
delete fakeIdToId[fakeId];
}
}
93 changes: 93 additions & 0 deletions src/hacktimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
let fakeIdToCallback = {};

function uuidv4() {
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) =>
(
c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))
).toString(16)
);
}

window.setInterval = function (callback, time /* , parameters */) {
Wikidepia marked this conversation as resolved.
Show resolved Hide resolved
let fakeId = uuidv4();
fakeIdToCallback[fakeId] = {
callback: callback,
parameters: Array.prototype.slice.call(arguments, 2),
};
chrome.runtime.sendMessage({
type: 'HackTimer',
label: {
name: 'setInterval',
fakeId,
time,
},
});
return fakeId;
};
window.clearInterval = function (fakeId) {
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
delete fakeIdToCallback[fakeId];
chrome.runtime.sendMessage({
type: 'HackTimer',
label: {
name: 'clearInterval',
fakeId,
},
});
}
};
window.setTimeout = function (callback, time /* , parameters */) {
let fakeId = uuidv4();
fakeIdToCallback[fakeId] = {
callback: callback,
parameters: Array.prototype.slice.call(arguments, 2),
isTimeout: true,
};
chrome.runtime.sendMessage({
type: 'HackTimer',
label: {
name: 'setTimeout',
fakeId,
time,
},
});
return fakeId;
};
window.clearTimeout = function (fakeId) {
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
delete fakeIdToCallback[fakeId];
chrome.runtime.sendMessage({
type: 'HackTimer',
label: {
name: 'clearTimeout',
fakeId,
},
});
}
};

chrome.runtime.onMessage.addListener(function (
{ type, label } /*, sender, sendResponse*/
) {
if (type === 'HackTimer') {
let fakeId = label.fakeId,
request,
parameters,
callback;
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
request = fakeIdToCallback[fakeId];
callback = request.callback;
parameters = request.parameters;
if (request.hasOwnProperty('isTimeout') && request.isTimeout) {
delete fakeIdToCallback[fakeId];
}
}
if (typeof callback === 'string') {
console.error('Callback is not supported as string');
}
if (typeof callback === 'function') {
callback.apply(window, parameters);
}
}
});
1 change: 1 addition & 0 deletions src/hcaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import 'jimp';
import { Time } from './utils';
require('./hacktimer.js');
const ort = require('onnxruntime-web');

// Modify ort wasm path
Expand Down
1 change: 1 addition & 0 deletions src/recaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import 'jimp';
import { KVStorage, Time } from './utils.js';

require('./hacktimer.js');
const ort = require('onnxruntime-web');

// Modify ort wasm path
Expand Down