From e7a1dad2fad89f5ac47d14d5a6e914342b69945b Mon Sep 17 00:00:00 2001 From: Nicolas Joseph Date: Thu, 14 May 2015 16:14:54 +0200 Subject: [PATCH] #7 - triggering clean up when the devtools panel is closed. --- extension/src/background.js | 7 ++++++- extension/src/injected/content-script.js | 8 ++++++++ extension/src/injected/inspector.js | 4 +++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/extension/src/background.js b/extension/src/background.js index e069fbb..2f3a669 100644 --- a/extension/src/background.js +++ b/extension/src/background.js @@ -156,9 +156,14 @@ chrome.runtime.onConnect.addListener(function(port){ port.onMessage.removeListener(panelListener); var tabs = Object.keys(panelConnections); - for (var i=0, len=tabs.length; i < len; i++) { + for (var i = 0, len = tabs.length; i < len ; i++) { if (panelConnections[tabs[i]] == port) { delete panelConnections[tabs[i]]; + // On panel closing, clean up the tab from all wrapped functions and removes the injector.js + contentScriptConnections[tabs[i]].postMessage({ + task: 'cleanUpInspectedApp', + source: 'angular-performance' + }); break; } } diff --git a/extension/src/injected/content-script.js b/extension/src/injected/content-script.js index e9e0ec0..5aa3b75 100644 --- a/extension/src/injected/content-script.js +++ b/extension/src/injected/content-script.js @@ -105,6 +105,13 @@ window.addEventListener('message', function(event) { return; } + if (message.task === 'removeInspector'){ + // removes the inspector from the DOM + var inspector = document.getElementById("angular-performance-inspector"); + inspector.parentNode.removeChild(inspector); + return; + } + backgroundPageConnection.postMessage(message); }, false); @@ -129,6 +136,7 @@ USER_EVENTS.forEach(function(eventType){ // Add injected script to the page var script = document.createElement('script'); script.type = 'text/javascript'; +script.id = 'angular-performance-inspector'; script.src = chrome.extension.getURL('src/injected/inspector.js'); document.head.appendChild(script); diff --git a/extension/src/injected/inspector.js b/extension/src/injected/inspector.js index d849c6b..aa4bab9 100644 --- a/extension/src/injected/inspector.js +++ b/extension/src/injected/inspector.js @@ -69,6 +69,8 @@ case 'cleanUpInspectedApp': _isMonitoringActive = false; cleanUpInspectedApp(); + // Once everything is cleaned up, we can remove this script from the DOM + sendTask('removeInspector'); break; } }); @@ -451,7 +453,7 @@ * Reports a metric * * @param {String} task - task to do - * @param {Object} value - data that can be sent along with the task + * @param {Object} [value] - data that can be sent along with the task */ function sendTask(task, value){ window.postMessage({