MikeCoder /
MJsonViewer
| 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 | let strictOnly; |
||
| 28 | let isDebug; |
||
| 29 | |||
| 30 | function onError (e) { |
||
| 31 | |||
| 32 | console.log(e); |
||
|
0 ignored issues
–
show
Debugging Code
introduced
by
Loading history...
|
|||
| 33 | |||
| 34 | } |
||
| 35 | |||
| 36 | function findJsonMimeType (header) { |
||
| 37 | |||
| 38 | if (strictOnly) { |
||
| 39 | |||
| 40 | if (isDebug) { |
||
| 41 | |||
| 42 | console.log('strict mode Only'); |
||
|
0 ignored issues
–
show
|
|||
| 43 | |||
| 44 | } |
||
| 45 | |||
| 46 | return false; |
||
| 47 | |||
| 48 | } |
||
| 49 | |||
| 50 | if (header.name === undefined) { |
||
| 51 | |||
| 52 | return false; |
||
| 53 | |||
| 54 | } |
||
| 55 | |||
| 56 | return header.name.toLowerCase() === 'content-type' && header.value.includes('json'); |
||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | function addJsonHandler (request) { |
||
| 61 | |||
| 62 | return new Promise((resolve) => { |
||
| 63 | |||
| 64 | if (request.responseHeaders.find(findJsonMimeType)) { |
||
| 65 | |||
| 66 | const jsonHeader = { |
||
| 67 | 'name': 'Content-Type', |
||
| 68 | 'value': 'application/json', |
||
| 69 | }; |
||
| 70 | request.responseHeaders.push(jsonHeader); |
||
| 71 | |||
| 72 | } |
||
| 73 | |||
| 74 | resolve({ |
||
| 75 | 'responseHeaders': request.responseHeaders, |
||
| 76 | }); |
||
| 77 | |||
| 78 | }); |
||
| 79 | |||
| 80 | } |
||
| 81 | |||
| 82 | function onConfig (result) { |
||
| 83 | |||
| 84 | if (!result) { |
||
| 85 | |||
| 86 | strictOnly = false; |
||
| 87 | isDebug = false; |
||
| 88 | |||
| 89 | } |
||
| 90 | if (result && result[0]) { |
||
| 91 | |||
| 92 | strictOnly = result[0].strictOnly || false; |
||
| 93 | isDebug = result[0].isDebug || false; |
||
| 94 | |||
| 95 | } else { |
||
| 96 | |||
| 97 | strictOnly = result.strictOnly || false; |
||
| 98 | isDebug = result.isDebug || false; |
||
| 99 | |||
| 100 | } |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | function onRecv () { |
||
| 105 | |||
| 106 | browser.webRequest.onHeadersReceived.addListener( |
||
| 107 | addJsonHandler, { |
||
| 108 | 'urls': ['<all_urls>', ], |
||
| 109 | }, [ |
||
| 110 | 'blocking', |
||
| 111 | 'responseHeaders', |
||
| 112 | ] |
||
| 113 | ); |
||
| 114 | |||
| 115 | } |
||
| 116 | |||
| 117 | browser.storage.onChanged.addListener(function (changes, areaName) { |
||
| 118 | |||
| 119 | if (areaName != 'local') { |
||
| 120 | |||
| 121 | return; |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 | isDebug = changes.isDebug.newValue; |
||
| 126 | strictOnly = changes.strictOnly.newValue; |
||
| 127 | |||
| 128 | }); |
||
| 129 | |||
| 130 | const getting = browser.storage.local.get(); |
||
| 131 | getting.then(onConfig, onError).then(onRecv, onError); |
||
| 132 |