Skip to content

Commit b27afe1

Browse files
authored
Merge pull request #79 from aquinojardim/bugs
Debugged tab apply new url
2 parents 88e33ed + a501754 commit b27afe1

File tree

4 files changed

+48
-21
lines changed

4 files changed

+48
-21
lines changed

MyMovie.gif

1.06 MB
Loading

ShortMovie.gif

446 KB
Loading

src/extension/background.js

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
244244
tabsObj[tabId].snapshots.splice(1);
245245
// gabi :: reset hierarchy to page initial state
246246
if (tabsObj[tabId].hierarchy) {
247+
//test
247248
tabsObj[tabId].hierarchy.children = [];
248249
// gabi :: reset currParent plus current state
249250
tabsObj[tabId].currParent = 1;
@@ -278,26 +279,6 @@ chrome.runtime.onMessage.addListener((request, sender) => {
278279
firstSnapshotReceived[tabId] = true;
279280
reloaded[tabId] = false;
280281

281-
// if (tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1]) {
282-
// let sameState = true;
283-
// const testState = (array, compare) => {
284-
// array.forEach((element, elIndex) => {
285-
// const test1 = JSON.stringify(element.state);
286-
// const test2 = JSON.stringify(compare[elIndex].state);
287-
// if (JSON.stringify(element.state) !== JSON.stringify(compare[elIndex].state)) {
288-
// sameState = false;
289-
// }
290-
// if (element.children) {
291-
// testState(element.children, compare[elIndex].children);
292-
// }
293-
// });
294-
// };
295-
// testState(tabsObj[tabId].snapshots[tabsObj[tabId].snapshots.length - 1].children, request.payload.children);
296-
// if (sameState) {
297-
// break;
298-
// }
299-
// }
300-
301282
tabsObj[tabId].snapshots.push(request.payload);
302283

303284
sendToHierarchy(
@@ -362,6 +343,49 @@ chrome.tabs.onRemoved.addListener(tabId => {
362343
delete firstSnapshotReceived[tabId];
363344
});
364345

346+
// when a new url is loaded on the same tab, this remove the tabid from the tabsObj, recreate the tab and inject the script
347+
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
348+
349+
// check if the tab title changed to see if tab need to restart
350+
if (changeInfo && tabsObj[tabId]){
351+
if(changeInfo.title && changeInfo.title !== tabsObj[tabId].title){
352+
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+
}
386+
}
387+
});
388+
365389
// when tab is view change, put the tabid as the current tab
366390
chrome.tabs.onActivated.addListener(info => {
367391
// tell devtools which tab to be the current

src/extension/contentScript.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ window.addEventListener('message', msg => { // runs automatically every second
99
// messages sent by contentscrip
1010
if (firstMessage) {
1111
// tell the background script that the tab has reloaded
12-
chrome.runtime.sendMessage({ action: 'tabReload' });
12+
chrome.runtime.sendMessage({
13+
action: 'tabReload',
14+
payload: msg
15+
});
1316
firstMessage = false;
1417
}
1518

0 commit comments

Comments
 (0)