1
0
forked from icd/rentgen

fix: modify manifest.json during build to inject test content script

This commit is contained in:
Jacek Wielemborek 2025-10-27 20:40:45 +00:00
parent af50636b3b
commit 865504ed6a
3 changed files with 50 additions and 22 deletions

View File

@ -112,4 +112,4 @@ Każdy problem zostanie sprawdzony i przeniesiony na wewnętrzną listę problem
--- ---
# Test pre-commit hook # Test pre-commit hook - without Docker check

View File

@ -1,5 +1,7 @@
import esbuild from 'esbuild'; import esbuild from 'esbuild';
import scss from 'esbuild-plugin-sass'; import scss from 'esbuild-plugin-sass';
import fs from 'fs';
import path from 'path';
const watch = process.argv.includes('--watch') && { const watch = process.argv.includes('--watch') && {
onRebuild(error) { onRebuild(error) {
@ -73,5 +75,34 @@ esbuild
external: ['react', 'react-dom', 'survey-react'], external: ['react', 'react-dom', 'survey-react'],
watch, watch,
}) })
.then(() => console.log('Add-on was built')) .then(() => {
console.log('Add-on was built');
// Modify manifest.json to include test content script when ENABLE_TESTS=true
if (ENABLE_TESTS) {
const manifestPath = path.join(process.cwd(), 'manifest.json');
const manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
// Add content_scripts for testing
if (!manifest.content_scripts) {
manifest.content_scripts = [];
}
// Check if test script is already added
const hasTestScript = manifest.content_scripts.some(
cs => cs.js && cs.js.includes('lib/tests/test-content-script.js')
);
if (!hasTestScript) {
manifest.content_scripts.push({
matches: ['<all_urls>'],
js: ['lib/tests/test-content-script.js'],
run_at: 'document_start'
});
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 4));
console.log('Added test content script to manifest.json');
}
}
})
.catch(() => process.exit(1)); .catch(() => process.exit(1));

View File

@ -2,24 +2,21 @@
// This JavaScript code runs in the browser context via Marionette // This JavaScript code runs in the browser context via Marionette
/** /**
* Inject test content script into the page * Check if test content script is loaded
* @returns {Promise<boolean>} - True if injection successful * The content script is automatically injected by manifest.json when ENABLE_TESTS=true
* @returns {Promise<boolean>} - True if content script is loaded
*/ */
async function injectTestContentScript() { async function waitForTestContentScript() {
// Read the content script file // Wait for content script to set the marker
const response = await fetch(browser.runtime.getURL('lib/tests/test-content-script.js')); let attempts = 0;
const scriptCode = await response.text(); while (attempts < 50) {
if (document.body && document.body.getAttribute('data-rentgen-injected') === 'true') {
// Inject it into the page return true;
const script = document.createElement('script'); }
script.textContent = scriptCode; await new Promise(resolve => setTimeout(resolve, 100));
document.documentElement.appendChild(script); attempts++;
script.remove(); }
return false;
// Wait a bit for script to initialize
await new Promise(resolve => setTimeout(resolve, 100));
return document.body.getAttribute('data-rentgen-injected') === 'true';
} }
/** /**
@ -28,9 +25,9 @@ async function injectTestContentScript() {
* @returns {Promise<number|null>} - Badge count (number of third-party domains) or null on failure * @returns {Promise<number|null>} - Badge count (number of third-party domains) or null on failure
*/ */
async function testBadgeCount() { async function testBadgeCount() {
// Inject content script first // Wait for content script to be loaded
const injected = await injectTestContentScript(); const loaded = await waitForTestContentScript();
if (!injected) { if (!loaded) {
return -1; // Content script not loaded return -1; // Content script not loaded
} }