forked from drnorthcott/chrome-default-encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
46 lines (36 loc) · 1.44 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* chrome-default-encoding
*
* background.js
* Adds a listener to onHeadersRecieved to convert all meta tags with content-type=text/html
* to add/override charset=utf8.
*
* uses google storage api for extensions to save the url pattern and default encoding you want to set
*
* if a meta tag already specifies an encoding it is not overridden
*
*/
var default_encoding = 'utf8'
var url_pattern = '*://*/*'
function load_configs() {
chrome.storage.sync.get({
default_encoding: 'UTF-8',
url_pattern: '*://*/*',
}, function(configs){
default_encoding = configs.default_encoding;
url_pattern = configs.url_pattern;
})
}
var initialize_configs = load_configs();
chrome.storage.onChanged.addListener(load_configs)
chrome.webRequest.onHeadersReceived.addListener(function(details){
for(var i = 0; i < details.responseHeaders.length; ++i)
if( details.responseHeaders[i].name.toLowerCase() == 'content-type' &&
details.responseHeaders[i].value.includes('text/html') &&
!details.responseHeaders[i].value.includes('charset') ) {
new_val = `text/html; charset=${default_encoding}`
console.log("%s: Exchanging '%s' with '%s'", i, details.responseHeaders[i].value, new_val);
details.responseHeaders[i].value = new_val;
}
return {responseHeaders:details.responseHeaders};
}, {urls: [url_pattern]}, ['blocking', 'responseHeaders']);