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 scss from 'esbuild-plugin-sass';
import fs from 'fs';
import path from 'path';
const watch = process.argv.includes('--watch') && {
onRebuild(error) {
@ -73,5 +75,34 @@ esbuild
external: ['react', 'react-dom', 'survey-react'],
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));

View File

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