Skip to content

Commit 74e20bb

Browse files
committed
Reload hack to let the RSS feed reader work on ESR60
1 parent d03e810 commit 74e20bb

1 file changed

Lines changed: 54 additions & 13 deletions

File tree

src/bg/RequestUtil.js

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,37 @@
44
let DEFAULT_CHARSET = "utf-8";
55
let xmlFeedOrImage = /^(?:(?:application|text)\/(?:(?:r(?:ss|df)|atom)\+)xml(;|$))|image\//i;
66
let rawXml = /^(?:application|text)\/xml;/i;
7-
let brokenOnLoad = (async () => parseInt(await browser.runtime.getBrowserInfo().version) < 61);
7+
let brokenOnLoad;
88

99
let pendingRequests = new Map();
1010

11+
let reloadingTabs = new Set();
12+
let tabKey = (tabId, url) => `${tabId}:${url}`;
13+
1114
let cleanup = r => {
1215
pendingRequests.delete(r.requestId);
1316
};
1417
let filter = {
1518
urls: ["<all_urls>"],
1619
types: ["main_frame", "sub_frame", "object"]
1720
};
18-
browser.webRequest.onCompleted.addListener(cleanup, filter);
21+
22+
23+
browser.webRequest.onCompleted.addListener(r => {
24+
cleanup(r);
25+
let {tabId, url} = r;
26+
let key = tabKey(tabId, url);
27+
if (reloadingTabs.has(key)) {
28+
debug("Reloading tab", key);
29+
browser.tabs.update(tabId, {url});
30+
}
31+
}, filter);
1932
browser.webRequest.onErrorOccurred.addListener(cleanup, filter);
2033

2134
let executeAll = async (scripts, where) => {
2235
let {url, tabId, frameId} = where;
36+
37+
let count = 0;
2338
for (let details of scripts.values()) {
2439
details = Object.assign({
2540
runAt: "document_start",
@@ -28,11 +43,13 @@
2843
}, details);
2944
try {
3045
await browser.tabs.executeScript(tabId, details);
46+
count++;
3147
debug("Execute on start OK", url, details);
3248
} catch (e) {
3349
error(e, "Execute on start failed", url, details);
3450
}
3551
}
52+
return count;
3653
};
3754

3855
var RequestUtil = {
@@ -42,7 +59,8 @@
4259
},
4360

4461
async executeOnStart(request, details) {
45-
let {requestId, tabId, frameId, statusCode} = request;
62+
let {requestId, url, tabId, frameId, statusCode} = request;
63+
4664
if (statusCode >= 300 && statusCode < 400) return;
4765
let scripts = pendingRequests.get(requestId);
4866
let scriptKey = JSON.stringify(details);
@@ -54,35 +72,57 @@
5472
return;
5573
}
5674

75+
if (frameId === 0) {
76+
let key = tabKey(tabId, url);
77+
debug("Checking whether %s is a reloading tab...", key);
78+
if (reloadingTabs.has(key)) {
79+
debug("Skipping dynamic script injection for reloading feed tab", key);
80+
let filter = browser.webRequest.filterResponseData(requestId);
81+
filter.onstart = e => {
82+
reloadingTabs.delete(key);
83+
filter.write(NULL);
84+
filter.disconnect();
85+
}
86+
return;
87+
}
88+
}
89+
5790
let content = this.getContentMetaData(request);
58-
debug(request.url, content.type, content.charset);
91+
debug(url, content.type, content.charset);
5992
if (xmlFeedOrImage.test(content.type) && !/\/svg\b/i.test(content.type)) return;
60-
let disconnect = !(brokenOnLoad && rawXml.test(content.type));
93+
if (typeof brokenOnLoad === "undefined") {
94+
brokenOnLoad = await (async () => parseInt((await browser.runtime.getBrowserInfo()).version) < 61)();
95+
}
96+
97+
let mustCheckFeed = brokenOnLoad && frameId === 0 && rawXml.test(content.type);
98+
debug("mustCheckFeed = %s, brokenOnLoad = %s", mustCheckFeed, brokenOnLoad);
6199
let filter = browser.webRequest.filterResponseData(requestId);
62100
let buffer = [];
63-
64101
let first = true;
65102
let runAndFlush = async () => {
66-
await executeAll(scripts, request);
67-
if (buffer.length) {
103+
let scriptsRan = await executeAll(scripts, request);
104+
if (mustCheckFeed && !scriptsRan) {
105+
debug(`Marking as "must reload"`, tabId, url);
106+
reloadingTabs.add(tabKey(tabId, url));
107+
}
108+
if (buffer && buffer.length) {
68109
debug("Flushing %s buffer chunks", buffer.length);
69110
for (let chunk of buffer) {
70111
filter.write(chunk);
71112
}
72-
if (disconnect) filter.disconnect();
113+
filter.disconnect();
73114
buffer = null;
74115
}
75116
};
76117

77118
if (brokenOnLoad) {
78119
filter.onstart = event => {
79-
debug(`onstart ${request.url}`);
80120
filter.write(NULL);
121+
debug("onstart", url);
81122
}
82123
}
83124

84125
filter.ondata = event => {
85-
86126
if (first) {
87127
runAndFlush();
88128
first = false;
@@ -92,11 +132,12 @@
92132
return;
93133
}
94134

95-
debug(`ondata ${request.url}`);
135+
debug("ondata", url);
96136
filter.write(event.data);
97-
if (disconnect) filter.disconnect();
137+
filter.disconnect();
98138
};
99139

140+
100141
}
101142
}
102143
}

0 commit comments

Comments
 (0)