Skip to content

Commit

Permalink
fix tab onHistoryStateUpdated + clean RequestManager
Browse files Browse the repository at this point in the history
  • Loading branch information
vico - low web committed Jan 28, 2020
1 parent da4571c commit 09a72ad
Showing 1 changed file with 61 additions and 41 deletions.
102 changes: 61 additions & 41 deletions src/scripts/background/RequestManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,23 @@ class RequestManager {
return this.tabStorage[browser.tabs.getCurrent()];
}
init() {
browser.webRequest.onBeforeRequest.addListener(
details => {
const { tabId, requestId } = details;
if (!this.tabStorage.hasOwnProperty(tabId)) {
this.addTab(tabId);
}
if (this.tabStorage[tabId.toString()]) {
this.tabStorage[tabId.toString()].requests[requestId] = {
requestId: requestId,
url: details.url,
startTime: details.timeStamp,
status: 'pending',
};
}
return {};
},
this.networkFilters,
['blocking']
);
const cbRequestOnBefore = details => {
const { tabId, requestId } = details;
if (!this.tabStorage.hasOwnProperty(tabId)) {
this.addTab(tabId);
}
if (this.tabStorage[tabId.toString()]) {
this.tabStorage[tabId.toString()].requests[requestId] = {
requestId: requestId,
url: details.url,
startTime: details.timeStamp,
status: 'pending',
};
}
return {};
};

browser.webRequest.onCompleted.addListener(details => {
const cbRequestCompleted = details => {
const { tabId, requestId } = details;
if (!this.tabStorage.hasOwnProperty(tabId.toString()) || !this.tabStorage[tabId.toString()].requests.hasOwnProperty(requestId)) {
return;
Expand All @@ -45,9 +41,9 @@ class RequestManager {
requestDuration: details.timeStamp - request.startTime,
status: 'complete',
});
}, this.networkFilters);
};

browser.webRequest.onErrorOccurred.addListener(details => {
const cbRequestErrorOccured = details => {
const { tabId, requestId } = details;
if (!this.tabStorage.hasOwnProperty(tabId.toString()) || !this.tabStorage[tabId.toString()].requests.hasOwnProperty(requestId)) {
return;
Expand All @@ -57,37 +53,61 @@ class RequestManager {
endTime: details.timeStamp,
status: 'error',
});
}, this.networkFilters);
};

browser.tabs.onUpdated.addListener(tabId => {
const cbTabUpdated = tabId => {
this.addTab(tabId);
});
};

browser.webNavigation.onBeforeNavigate.addListener(info => {
if (info.frameId === 0) {
const hostname = getHostname(info.url);
if (hostname) {
if (!this.tabStorage[info.tabId]) {
this.addTab(info.tabId);
}
this.tabStorage[info.tabId].pageUrl = info.url;
this.tabStorage[info.tabId].domain = hostname;
}
}
});
const cbNavigationCommitted = info => {
this.updateTabUrl(info);
};

browser.tabs.onActivated.addListener(tab => {
const cbNavigationHistoryUpdated = info => {
this.updateTabUrl(info);
};

const cbNavigationBeforeNavigate = info => {
this.updateTabUrl(info);
};

const cbTabActivated = tab => {
const tabId = tab ? tab.tabId : browser.tabs.TAB_ID_NONE;
this.currentTabId = tabId;
this.addTab(tabId);
});
};

browser.tabs.onRemoved.addListener(tabId => {
const cbTabRemoved = tabId => {
if (!this.tabStorage.hasOwnProperty(tabId)) {
return;
}
delete this.tabStorage[tabId];
});
};

browser.webRequest.onBeforeRequest.addListener(cbRequestOnBefore, this.networkFilters);
browser.webRequest.onCompleted.addListener(cbRequestCompleted, this.networkFilters);
browser.webRequest.onErrorOccurred.addListener(cbRequestErrorOccured, this.networkFilters);

// TODO look to add filters
browser.webNavigation.onCommitted.addListener(cbNavigationCommitted);
browser.webNavigation.onHistoryStateUpdated.addListener(cbNavigationHistoryUpdated);
browser.webNavigation.onBeforeNavigate.addListener(cbNavigationBeforeNavigate);

browser.tabs.onUpdated.addListener(cbTabUpdated);
browser.tabs.onActivated.addListener(cbTabActivated);
browser.tabs.onRemoved.addListener(cbTabRemoved);
}
updateTabUrl(info) {
if (info.frameId === 0) {
const hostname = getHostname(info.url);
if (hostname) {
if (!this.tabStorage[info.tabId]) {
this.addTab(info.tabId);
}
this.tabStorage[info.tabId].pageUrl = info.url;
this.tabStorage[info.tabId].domain = hostname;
}
}
}
addTab(tabId) {
if (tabId && !this.tabStorage.hasOwnProperty(tabId) && tabId !== -1) {
Expand Down

0 comments on commit 09a72ad

Please sign in to comment.