Skip to content

Commit

Permalink
Merge pull request #56 from oslabs-beta/austin/remove-console-logs
Browse files Browse the repository at this point in the history
Austin/remove console logs
  • Loading branch information
johnwdunn20 authored May 9, 2024
2 parents a4ed265 + d1300d9 commit 03281a0
Show file tree
Hide file tree
Showing 18 changed files with 204 additions and 187 deletions.
46 changes: 23 additions & 23 deletions extension/background.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
console.log("BACKGROUND.TS: Loaded");
// console.log("BACKGROUND.TS: Loaded");

let devToolPort: chrome.runtime.Port | null = null;
let activeContentPort: chrome.runtime.Port | null = null;
Expand All @@ -8,11 +8,11 @@ let devToolMessageQueue: any = [];
let contentMessageQueue: any = [];

// Listen for connection from content.ts and devtools panel
chrome.runtime.onConnect.addListener((port) => {
console.log("BACKGROUND.TS: Connection established: ", port);
if (port.name === "content-background") {
chrome.runtime.onConnect.addListener(port => {
// console.log('BACKGROUND.TS: Connection established: ', port);
if (port.name === 'content-background') {
handleContentConnection(port);
} else if (port.name === "background-devtool") {
} else if (port.name === 'background-devtool') {
handleDevToolsConnection(port);
}
});
Expand All @@ -37,28 +37,28 @@ function handleContentConnection(port: chrome.runtime.Port) {
contentMessageQueue = [];

// If devtool is connected send messages otherwise place in queue
activeContentPort.onMessage.addListener((message) => {
activeContentPort.onMessage.addListener(message => {
// The background script goes inactive after 30 seconds idle so we log every 25 seconds
if (message.type === "heartbeat") {
console.log("BACKGROUND.TS: Logging to keep service worker connected");
if (message.type === 'heartbeat') {
// console.log('BACKGROUND.TS: Logging to keep service worker connected');
}

if (devToolPort) {
console.log("BACKGROUND.TS: Message to dev tool", message);
// console.log('BACKGROUND.TS: Message to dev tool', message);
devToolPort.postMessage(message);
} else {
devToolMessageQueue.push(message);
}
});

port.onDisconnect.addListener(() => {
console.log("BACKGROUND.TS: Content.ts disconnected");
// console.log('BACKGROUND.TS: Content.ts disconnected');
activeContentPort = null;
});
}

function handleDevToolsConnection(port: chrome.runtime.Port) {
console.log("BACKGROUND.TS: DevTool connected");
// console.log('BACKGROUND.TS: DevTool connected');
devToolPort = port;

// Send queued messages from the devtool before connection was established
Expand All @@ -68,22 +68,22 @@ function handleDevToolsConnection(port: chrome.runtime.Port) {
devToolMessageQueue = [];

// If content.ts is connected send messages otherwise place in queue
devToolPort.onMessage.addListener((message) => {
if (message.type === "profiling-status") {
console.log("BACKGROUND.TS: Profiling status", message);
devToolPort.onMessage.addListener(message => {
if (message.type === 'profiling-status') {
// console.log('BACKGROUND.TS: Profiling status', message);
}
console.log('Injecting content.js into tab with message: ', message);
if (message.action === "injectContentScript" && message.tabId) {
console.log(
"BACKGROUND.TS: Injecting content script into tab:",
message.tabId
);
// console.log('Injecting content.js into tab with message: ', message);
if (message.action === 'injectContentScript' && message.tabId) {
// console.log(
// 'BACKGROUND.TS: Injecting content script into tab:',
// message.tabId
// );
chrome.scripting.executeScript({
target: { tabId: message.tabId },
files: ["content.js"],
files: ['content.js'],
});
} else if (activeContentPort) {
console.log("BACKGROUND.TS: Message to content.ts", message);
// console.log('BACKGROUND.TS: Message to content.ts', message);
activeContentPort.postMessage(message);
} else {
// console.log('BACKGROUND.TS: Message added to content.ts queue');
Expand All @@ -92,7 +92,7 @@ function handleDevToolsConnection(port: chrome.runtime.Port) {
});

port.onDisconnect.addListener(() => {
console.log("BACKGROUND.TS: DevTool disconnected");
// console.log('BACKGROUND.TS: DevTool disconnected');
devToolPort = null;
});
}
80 changes: 40 additions & 40 deletions extension/content_scripts/content.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
// Function to inject the script into the current tab
const inject = () => {
let isInjected = false;

return function(fileName: string) {
if (!isInjected) {
if (document.getElementById("treeScript-jkhsdfkdshdsf")) {
console.log('Tree script already injected');
return;
}
const treeScript = document.createElement("script");
// Adding a unique id to the script tag to prevent it from being injected multiple times because my closure isn't working for some reason (not sure if it's because of how content scripts work or something else)
treeScript.id = "treeScript-jkhsdfkdshdsf";
treeScript.setAttribute("type", "text/javascript");
treeScript.setAttribute("src", chrome.runtime.getURL(fileName));
document.body.appendChild(treeScript);
isInjected = true;
console.log('Injected tree script');
} else {
console.log('Tree script already injected');
// Function to inject the script into the current tab
const inject = () => {
let isInjected = false;

return function (fileName: string) {
if (!isInjected) {
if (document.getElementById('treeScript-jkhsdfkdshdsf')) {
// console.log('Tree script already injected');
return;
}
const treeScript = document.createElement('script');
// Adding a unique id to the script tag to prevent it from being injected multiple times because my closure isn't working for some reason (not sure if it's because of how content scripts work or something else)
treeScript.id = 'treeScript-jkhsdfkdshdsf';
treeScript.setAttribute('type', 'text/javascript');
treeScript.setAttribute('src', chrome.runtime.getURL(fileName));
document.body.appendChild(treeScript);
isInjected = true;
// console.log('Injected tree script');
} else {
// console.log('Tree script already injected');
}
};
};

// Immediatly-Invoked Function Expression (IIFE)
(function () {
// Check if the content script has already been loaded into the current tab
// Prevents it from injecting into the same page twice if the developer opens and closes the dev tool
if (window.myContentScriptLoaded) {
console.log("CONTENT.TS: Content script already injected");
// console.log("CONTENT.TS: Content script already injected");
return;
} else {
// Set the flag on the window to indicate the content script has already been loaded in the tab
window.myContentScriptLoaded = true;
console.log("CONTENT.TS: Loaded");
// console.log("CONTENT.TS: Loaded");
}

let appConnected = false;
Expand All @@ -44,21 +44,21 @@

// Function to setup and initialize the background port
function setupPort() {
console.log("CONTENT.TS: Background.ts Connected");
// console.log("CONTENT.TS: Background.ts Connected");

// Connect to background script
backgroundPort = chrome.runtime.connect({ name: "content-background" });
backgroundPort = chrome.runtime.connect({ name: 'content-background' });

// Handle background.ts messages - send message if connected to app otherwise add to queue
backgroundPort.onMessage.addListener((message) => {
console.log("CONTENT.TS: BackgroundPort.OnMessage: ", message.data?.type);
backgroundPort.onMessage.addListener(message => {
// console.log("CONTENT.TS: BackgroundPort.OnMessage: ", message.data?.type);
if (appConnected) {
console.log("CONTENT.TS: Message to app", message);
// console.log("CONTENT.TS: Message to app", message);
// Inject script to get react tree data
if (message.type === "profiling-status") {
console.log('tree script *should* be injected');
if (message.type === 'profiling-status') {
// console.log('tree script *should* be injected');
const scriptToInject = inject();
scriptToInject("inject.js");
scriptToInject('inject.js');
// return so message isn't posted anywhere
return;
}
Expand All @@ -69,7 +69,7 @@
});

backgroundPort.onDisconnect.addListener(() => {
console.log("CONTENT.TS: Background.ts Disconnected");
// console.log('CONTENT.TS: Background.ts Disconnected');
// Reset the port to trigger reconnection attempt
backgroundPort = null;
setupPort();
Expand All @@ -82,22 +82,22 @@
}

// Add listener to the window to handle messages from the app
window.addEventListener("message", handleMessageFromApp, false);
window.addEventListener('message', handleMessageFromApp, false);

function handleMessageFromApp(message: MessageEvent) {
console.log("CONTENT.TS: handleMessageFromApp", message.data?.type);
// console.log('CONTENT.TS: handleMessageFromApp', message.data?.type);
// Initial message from the app to confirm connection
if (message.data?.type === "app-connected") {
console.log("CONTENT.TS: App Connected");
if (message.data?.type === 'app-connected') {
// console.log('CONTENT.TS: App Connected');
clearInterval(appConnectionInterval);
appConnected = true;
appMessageQueue.forEach((message: any) => window.postMessage(message));
appMessageQueue = [];
}

// Send tree data to background.ts
if (message.data.type && message.data.type === "tree") {
console.log("CONTENT.ts: component tree sending event: ", message);
if (message.data.type && message.data.type === 'tree') {
// console.log('CONTENT.ts: component tree sending event: ', message);
backgroundPort?.postMessage({
type: message.data.type,
data: JSON.parse(message.data.eventListStr),
Expand All @@ -106,7 +106,7 @@
}

// All other messages are sent to background.ts
if (message.data?.type === "event") {
if (message.data?.type === 'event') {
// console.log('CONTENT.TS: Message from App:', message);
sendMessageToBackground(message);
}
Expand All @@ -115,7 +115,7 @@
// Notify app that content.ts is ready
function establishAppConnection() {
if (!appConnected) {
window.postMessage({ type: "content-script-ready" }, "*");
window.postMessage({ type: 'content-script-ready' }, '*');
}
}

Expand All @@ -125,7 +125,7 @@

// Function to send a heartbeat message to the background script to keep it active
function sendHeartbeat() {
backgroundPort?.postMessage({ type: "heartbeat" });
backgroundPort?.postMessage({ type: 'heartbeat' });
// console.log('heartbeat');
}

Expand Down
Loading

0 comments on commit 03281a0

Please sign in to comment.