Skip to content

Commit 684120a

Browse files
committed
implement if statement onUpdated function
1 parent 640df47 commit 684120a

File tree

1 file changed

+35
-33
lines changed

1 file changed

+35
-33
lines changed

src/extension/background.js

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -347,40 +347,42 @@ chrome.tabs.onRemoved.addListener(tabId => {
347347
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
348348

349349
// check if the tab title changed to see if tab need to restart
350-
if (changeInfo.title && changeInfo.title !== tabsObj[tabId].title){
351-
352-
// tell devtools which tab to delete
353-
if (portsArr.length > 0) {
354-
portsArr.forEach(bg =>
355-
bg.postMessage({
356-
action: 'deleteTab',
357-
payload: tabId,
358-
}),
359-
);
360-
}
350+
if (changeInfo && tabsObj[tabId]){
351+
if(changeInfo.title && changeInfo.title !== tabsObj[tabId].title){
361352

362-
// delete the tab from the tabsObj
363-
delete tabsObj[tabId];
364-
delete reloaded[tabId];
365-
delete firstSnapshotReceived[tabId];
366-
367-
// recreate the tab on the tabsObj
368-
tabsObj[tabId] = createTabObj(changeInfo.title);
369-
370-
// reinject the script to the tab
371-
chrome.tabs.executeScript(tabId, {
372-
code: `
373-
// Function will attach script to the dom
374-
const injectScript = (file, tag) => {
375-
const htmlBody = document.getElementsByTagName(tag)[0];
376-
const script = document.createElement('script');
377-
script.setAttribute('type', 'text/javascript');
378-
script.setAttribute('src', file);
379-
htmlBody.appendChild(script);
380-
};
381-
injectScript(chrome.runtime.getURL('bundles/backend.bundle.js'), 'body');
382-
`,
383-
});
353+
// tell devtools which tab to delete
354+
if (portsArr.length > 0) {
355+
portsArr.forEach(bg =>
356+
bg.postMessage({
357+
action: 'deleteTab',
358+
payload: tabId,
359+
}),
360+
);
361+
}
362+
363+
// delete the tab from the tabsObj
364+
delete tabsObj[tabId];
365+
delete reloaded[tabId];
366+
delete firstSnapshotReceived[tabId];
367+
368+
// recreate the tab on the tabsObj
369+
tabsObj[tabId] = createTabObj(changeInfo.title);
370+
371+
// reinject the script to the tab
372+
chrome.tabs.executeScript(tabId, {
373+
code: `
374+
// Function will attach script to the dom
375+
const injectScript = (file, tag) => {
376+
const htmlBody = document.getElementsByTagName(tag)[0];
377+
const script = document.createElement('script');
378+
script.setAttribute('type', 'text/javascript');
379+
script.setAttribute('src', file);
380+
htmlBody.appendChild(script);
381+
};
382+
injectScript(chrome.runtime.getURL('bundles/backend.bundle.js'), 'body');
383+
`,
384+
});
385+
}
384386
}
385387
});
386388

0 commit comments

Comments
 (0)