From 352389c0c6500cac49c29f8b573379429f718315 Mon Sep 17 00:00:00 2001 From: Rob Wu Date: Tue, 6 Feb 2018 15:06:02 +0100 Subject: [PATCH] [CRX] Expose some headers to fetch requests --- extensions/chromium/preserve-referer.js | 27 +++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/extensions/chromium/preserve-referer.js b/extensions/chromium/preserve-referer.js index 9e78bcb37..1651676e3 100644 --- a/extensions/chromium/preserve-referer.js +++ b/extensions/chromium/preserve-referer.js @@ -117,8 +117,17 @@ chrome.runtime.onConnect.addListener(function onReceivePort(port) { delete g_referrers[tabId][frameId]; } chrome.webRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders); + chrome.webRequest.onHeadersReceived.removeListener(exposeOnHeadersReceived); }); + // Expose some response headers for fetch API calls from PDF.js; + // This is a work-around for https://crbug.com/784528 + chrome.webRequest.onHeadersReceived.addListener(exposeOnHeadersReceived, { + urls: ['https://*/*'], + types: ['xmlhttprequest'], + tabId: tabId, + }, ['blocking', 'responseHeaders']); + function onBeforeSendHeaders(details) { if (details.frameId !== frameId) { return; @@ -137,4 +146,22 @@ chrome.runtime.onConnect.addListener(function onReceivePort(port) { refererHeader.value = referer; return { requestHeaders: headers, }; } + + function exposeOnHeadersReceived(details) { + if (details.frameId !== frameId) { + return; + } + var headers = details.responseHeaders; + var aceh = getHeaderFromHeaders(headers, 'access-control-expose-headers'); + // List of headers that PDF.js uses in src/display/network_utils.js + var acehValue = + 'accept-ranges,content-encoding,content-length,content-disposition'; + if (aceh) { + aceh.value += ',' + acehValue; + } else { + aceh = { name: 'Access-Control-Expose-Headers', value: acehValue, }; + headers.push(aceh); + } + return { responseHeaders: headers, }; + } });