Listener will stop, if there is no tpt object 10 seconds after page load

This commit is contained in:
Andi Petzoldt 2024-07-05 11:51:35 +02:00
parent 97e1ab9232
commit a928ad2198
3 changed files with 70 additions and 76 deletions

View File

@ -33,18 +33,6 @@ Configure the extension settings through the options page.
- Enter the JavaScript content to inject.
- Options to block Google Tag Manager and Tracking Proxy code based on specified criteria.
### Scripts and Files
- **content.js:** Injects scripts into the web page and handles messaging between the extension and the web page.
- **inject-listener.js:** Listens for specific messages to manage the Tracking Proxy.
- **inject-userscript.js:** Injects user scripts into the web page.
- **popup.html:** HTML structure for the extension's popup interface.
- **popup.js:** JavaScript to handle the logic for the popup interface.
- **options.html:** HTML structure for the extension's options page.
- **options.js:** JavaScript to handle the logic for the options page.
- **worker.js:** Service worker to manage background tasks and event tracking.
- **manifest.json:** Defines the extension's metadata, permissions, and configurations.
## Contributing
Feel free to contribute by submitting issues or pull requests. For significant changes, please open an issue first to discuss what you would like to change.
@ -52,3 +40,9 @@ Feel free to contribute by submitting issues or pull requests. For significant c
## License
This project is licensed under the MIT License.
## Changelog
- v1 05.07.2024*
- Initial Release

View File

@ -1,5 +1,61 @@
// inject-listener.js
//console.log('Injected listener loaded');
// Konsolen-Ausgaben für Debugging aktivieren oder deaktivieren
const debugEnabled = true;
const debugLog = (...args) => {
if (debugEnabled) {
console.log(...args);
}
};
debugLog('Injected listener loaded');
// Funktion, um Nachrichten an das globale Fenster zu senden
function sendTPmsg(o) {
window.postMessage(o, '*');
}
// Funktion, um __tpt-Details zu extrahieren und zurückzugeben
function getTPDetails() {
return window.__tpt ? JSON.parse(JSON.stringify(window.__tpt)) : {};
}
// Überprüfung des __tpt Objekts und Versendung des Ergebnisses
function checkTPT() {
const tpDetails = getTPDetails();
sendTPmsg({ type: 'CHECK_TPT_RESULT', hasTPT: !!tpDetails.cid, tpobj: tpDetails });
}
// Proxy Handler zur Überwachung von Änderungen
const handler = {
set(target, property, value) {
target[property] = value;
debugLog(`Property ${property} changed to`, value);
if (property === 'cid') {
checkTPT();
}
return true;
},
get(target, property) {
return target[property];
}
};
// Initialisierung des Proxies für das __tpt Objekt
if (window.__tpt) {
window.__tpt = new Proxy(window.__tpt, handler);
} else {
Object.defineProperty(window, '__tpt', {
set(value) {
if (value && typeof value === 'object') {
value = new Proxy(value, handler);
}
Object.defineProperty(this, '__tpt', { value, writable: true, configurable: true, enumerable: true });
checkTPT();
},
configurable: true,
enumerable: true
});
}
window.addEventListener('message', function(event) {
if (!event.data || !event.data.type || !event.data.details) return;
@ -17,70 +73,14 @@ window.addEventListener('message', function(event) {
}
}, false);
// Function to post messages
function sendTPmsg(o){
window.postMessage(o, '*');
}
function getTPDetails() {
return window.__tpt ? JSON.parse(JSON.stringify(window.__tpt)) : {};
}
// Function to check for TP and send message if available
function checkTPT() {
const tpDetails = getTPDetails();
sendTPmsg({ type: 'CHECK_TPT_RESULT', hasTPT: !!tpDetails.cid, tpobj: tpDetails });
}
// Proxy handler to detect changes
const handler = {
set(target, property, value) {
target[property] = value;
//console.log(`Property ${property} changed to`, value);
if (property === 'cid') {
checkTPT();
// Timeout zum Beenden des Listeners, wenn __tpt nicht innerhalb von 10 Sekunden erscheint
setTimeout(() => {
if (!window.__tpt || !window.__tpt.cid) {
debugLog('Kein __tpt Objekt gefunden oder keine CID nach 10 Sekunden.');
window.removeEventListener('message', this);
}
return true;
},
get(target, property) {
return target[property];
}
};
}, 10000);
// Wrap the __tpt object with a Proxy
if (window.__tpt) {
window.__tpt = new Proxy(window.__tpt, handler);
} else {
Object.defineProperty(window, '__tpt', {
set(value) {
if (value && typeof value === 'object') {
value = new Proxy(value, handler);
}
Object.defineProperty(this, '__tpt', { value, writable: true, configurable: true, enumerable: true });
checkTPT();
},
configurable: true,
enumerable: true
});
}
function exposeTPT() {
if (!window.__tpt) {
//console.log('No __tpt object found.');
return;
}
try {
// Deep clone the object to break references
const tpDetails = JSON.parse(JSON.stringify(window.__tpt));
//console.log('Current TP details:', tpDetails);
} catch (error) {
//console.error('Error exposing __tpt details:', error);
}
}
// Expose the function to global scope for easy access from the console
window.exposeTPT = exposeTPT;
/*
(function() {

View File

@ -9,7 +9,7 @@
"manifest_version": 3,
"name": "Tracking Proxy Helper",
"author": "andi@tracking-garden.com",
"version": "1.0",
"version": "1",
"description": "This extension gives some information and options using the Tracking Proxy.",
"icons": {
//"16": "/img/get_started16.png",