90 lines
3.5 KiB
JavaScript
90 lines
3.5 KiB
JavaScript
/***** Content JS for Chrome Extension: Tracking Proxy Helper *****/
|
|
|
|
/*** Initialisation ***/
|
|
let tpdm = { activeTabId: null, activeWindowId: null, extInit: false, tpl: { tpId: '', eventCount: 0 } };
|
|
tpdm.data = tpdm.data || JSON.parse(JSON.stringify(tpdm.tpl));
|
|
//console.log('content.js loaded');
|
|
|
|
/*** Logic ***/
|
|
|
|
// Function to inject javascript code into the Page DOM
|
|
// --> Accessing window object see: https://silverbirder.github.io/en-US/blog/contents/chrome_extension_development_feedback/
|
|
const injectScript = (filePath, id, tag) => {
|
|
if (!document.getElementById(id)) {
|
|
var node = document.getElementsByTagName(tag)[0];
|
|
var script = document.createElement("script");
|
|
script.setAttribute("type", "text/javascript");
|
|
script.setAttribute("src", filePath);
|
|
script.setAttribute("id", id);
|
|
node.appendChild(script);
|
|
}
|
|
};
|
|
|
|
// Listener to receive messages from the injected script
|
|
window.addEventListener('message', function(event) {
|
|
//console.log('Message received from injected script:', event);
|
|
if (event.data.type === 'CHECK_TPT_RESULT') {
|
|
//console.log('Message received from injected script:', event);
|
|
chrome.runtime.sendMessage({ type: 'CHECK_TPT', hasTPT: event.data.hasTPT, tpobj:event.data.tpobj });
|
|
}
|
|
if (event.data.type === 'TRACKING_PROXY_RESPONSE') {
|
|
//console.log('Message received from injected TP script:', event);
|
|
const eventData = event.data.details.eventData;
|
|
const metaData = event.data.details.metaData;
|
|
chrome.runtime.sendMessage({ type: 'TRACKING_PROXY_EVENT', details: { eventData:eventData, metaData:metaData } });
|
|
}
|
|
});
|
|
|
|
// Listener for messages from the background script
|
|
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
|
if (message.type === 'TRACKING_PROXY_REQUEST') {
|
|
window.postMessage({ type: 'TRACKING_PROXY_REDIRECT', details: message.details }, '*');
|
|
}
|
|
});
|
|
|
|
|
|
// Inject script on document load
|
|
if (document.readyState === 'complete' || document.readyState === 'interactive') {
|
|
injectScript(chrome.runtime.getURL("inject-listener.js"), "tp_dev_mode_script", "head");
|
|
} else {
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
injectScript(chrome.runtime.getURL("inject-listener.js"), "tp_dev_mode_script", "head");
|
|
});
|
|
}
|
|
|
|
chrome.storage.sync.get(['scriptContent', 'scriptUrl', 'scriptUrlIsRegex'], function(data) {
|
|
const scriptContent = data.scriptContent || '';
|
|
const scriptUrl = data.scriptUrl || '';
|
|
const scriptUrlIsRegex = data.scriptUrlIsRegex || false;
|
|
|
|
if (!scriptUrl || !scriptContent) {
|
|
//console.log('No script URL or script content defined, no script injected');
|
|
return;
|
|
}
|
|
|
|
let urlMatches;
|
|
if (scriptUrlIsRegex) {
|
|
const regex = new RegExp(scriptUrl);
|
|
urlMatches = regex.test(window.location.href);
|
|
} else {
|
|
urlMatches = window.location.href.includes(scriptUrl);
|
|
}
|
|
|
|
if (urlMatches) {
|
|
if (!document.getElementById('tp_user_script')) {
|
|
//console.log(`Injecting script into URL: ${window.location.href}`);
|
|
const script = document.createElement('script');
|
|
script.src = chrome.runtime.getURL('inject-userscript.js');
|
|
script.setAttribute("id", "tp_user_script");
|
|
(document.head || document.documentElement).appendChild(script);
|
|
script.onload = function() {
|
|
window.postMessage({ type: 'INJECT_USER_SCRIPT', scriptContent: scriptContent }, '*');
|
|
script.remove();
|
|
};
|
|
}
|
|
} else {
|
|
//console.log(`URL does not match, no script injected for URL ${window.location.href}`);
|
|
}
|
|
});
|
|
|
|
// EOF
|