Completed
Push — master ( d1ab70...a5681a )
by Dongxin
30s
created

background.js ➔ addJsonHandler   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 15
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A background.js ➔ ... ➔ ??? 0 13 2
1
//////////////////////////////////////////////////////////////////////////////////////
2
// Copyright © 2017 TangDongxin
3
//
4
// Permission is hereby granted, free of charge, to any person obtaining
5
// a copy of this software and associated documentation files (the "Software"),
6
// to deal in the Software without restriction, including without limitation
7
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
// and/or sell copies of the Software, and to permit persons to whom the
9
// Software is furnished to do so, subject to the following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
20
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
//////////////////////////////////////////////////////////////////////////////////////
22
23
// ===========================================
24
// JSON HEADER REWRITE
25
// ===========================================
26
27
'use strict';
28
29
const findJsonMimeType = function(header) {
30
    if (header.name === undefined) {
31
        return false;
32
    }
33
    return header.name.toLowerCase() === 'content-type' && header.value.includes('json');
34
};
35
36
const addJsonHandler = function(request) {
37
    return new Promise((resolve) => {
38
        if (request.responseHeaders.find(findJsonMimeType)) {
39
            const jsonHeader = {
40
                name: 'Content-Type',
41
                value: 'application/json'
42
            };
43
            request.responseHeaders.push(jsonHeader);
44
        }
45
46
        resolve({
47
            responseHeaders: request.responseHeaders
48
        });
49
    });
50
};
51
52
browser.webRequest.onHeadersReceived.addListener(
0 ignored issues
show
Bug introduced by
The variable browser seems to be never declared. If this is a global, consider adding a /** global: browser */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
53
    addJsonHandler, {
54
        urls: ['<all_urls>']
55
    }, [
56
        'blocking',
57
        'responseHeaders'
58
    ]
59
);
60