|
1 | | -function loadFromStorageAndCookies() { |
2 | | - // Populate cookies/LocalStorage combobox |
3 | | - function checkLoadJwtFromLength() { |
4 | | - var optGroups = [ |
5 | | - $('optgroup[label="Cookies"]'), |
6 | | - $('optgroup[label="Web Storage"]') |
7 | | - ]; |
8 | | - |
9 | | - optGroups.forEach(function(optGroup) { |
10 | | - var hasJWTs = |
11 | | - optGroup.children(':not(.load-from-no-jwts)').length > 0; |
12 | | - if(hasJWTs) { |
13 | | - optGroup.children('.load-from-no-jwts').remove(); |
14 | | - } else { |
15 | | - optGroup.empty(); |
16 | | - optGroup.append($('<option/>', { |
17 | | - 'class': 'load-from-no-jwts', |
18 | | - 'text': 'No JWTs found', |
19 | | - 'disabled': true |
20 | | - })); |
21 | | - } |
22 | | - }); |
| 1 | +import { isToken } from '../editor/jwt.js' |
| 2 | +import { getTokenEditorValue, setTokenEditorValue } from '../editor'; |
| 3 | +import { |
| 4 | + cookiesOptGroup, |
| 5 | + webStorageOptGroup, |
| 6 | + saveBackElement, |
| 7 | + saveBackLink, |
| 8 | + storageSelect |
| 9 | +} from './dom-elements.js'; |
| 10 | +import strings from '../strings.js'; |
| 11 | + |
| 12 | +function updateOptGroups() { |
| 13 | + var optGroups = [cookiesOptGroup, webStorageOptGroup]; |
| 14 | + |
| 15 | + optGroups.forEach(optGroup => { |
| 16 | + const hasJWTs = optGroup.querySelectorAll(':not(.load-from-no-jwts)') |
| 17 | + .length > 0; |
| 18 | + if (hasJWTs) { |
| 19 | + const toRemove = optGroup.querySelectorAll('.load-from-no-jwts'); |
| 20 | + Array.prototype.forEach.call(toRemove, e => e.remove()); |
| 21 | + } else { |
| 22 | + const noJwtOption = document.createElement('option'); |
| 23 | + noJwtOption.classList.add('load-from-no-jwts'); |
| 24 | + noJwtOption.text = strings.extension.noJwtsFound; |
| 25 | + noJwtOption.disabled = true; |
| 26 | + |
| 27 | + optGroup.innerHTML = ''; // Remove all elements |
| 28 | + optGroup.appendChild(noJwtOption); |
| 29 | + } |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +function messageHandler(message) { |
| 34 | + if (message.type !== 'cookies' && message.type !== 'storage') { |
| 35 | + return; |
23 | 36 | } |
24 | 37 |
|
25 | | - function jwtMessage(message) { |
26 | | - if(message.type !== 'cookies' && message.type !== 'storage') { |
| 38 | + const elements = []; |
| 39 | + |
| 40 | + message.tokens.forEach(token => { |
| 41 | + if (!isToken(token.value)) { |
| 42 | + if(message.type === 'cookies') { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + // Try again after parsing it first, some people do |
| 48 | + //localStorage.setItem('jwt', JSON.stringify(token)) |
| 49 | + token.value = JSON.parse(token.value); |
| 50 | + if (!isToken(token.value)) { |
| 51 | + // Not a valid token, ignore it. |
27 | 52 | return; |
| 53 | + } |
| 54 | + } catch (e) { |
| 55 | + // Not a valid token, ignore it. |
| 56 | + return; |
28 | 57 | } |
| 58 | + } |
29 | 59 |
|
30 | | - var elements = []; |
31 | | - |
32 | | - message.tokens.forEach(function(token) { |
33 | | - if(!isToken(token.value)) { |
34 | | - if(message.type === 'storage') { |
35 | | - try { |
36 | | - // Try again after parsing it first, some people do |
37 | | - //localStorage.setItem('jwt', JSON.stringify(token)) |
38 | | - token.value = JSON.parse(token.value); |
39 | | - if(!isToken(token.value)) { |
40 | | - return; |
41 | | - } |
42 | | - } catch(e) { |
43 | | - return; |
44 | | - } |
45 | | - } else { |
46 | | - return; |
47 | | - } |
48 | | - } |
49 | | - |
50 | | - var e = $('<option/>').text(token.name) |
51 | | - .val(token.value) |
52 | | - .data('type', token.type) |
53 | | - if(token.cookie) { |
54 | | - e.data('cookie', token.cookie); |
55 | | - } |
56 | | - elements.push(e); |
57 | | - }); |
| 60 | + const e = document.createElement('option'); |
| 61 | + e.text = token.name; |
| 62 | + e.value = token.value; |
| 63 | + e.setAttribute('data-type', token.type); |
58 | 64 |
|
59 | | - if(message.type === 'cookies') { |
60 | | - $('optgroup[label="Cookies"]').append(elements); |
61 | | - } else { |
62 | | - $('optgroup[label="Web Storage"]').append(elements); |
63 | | - } |
| 65 | + if(token.cookie) { |
| 66 | + e.setAttribute('data-cookie', JSON.stringify(token.cookie)); |
| 67 | + } |
| 68 | + |
| 69 | + elements.push(e); |
| 70 | + }); |
64 | 71 |
|
65 | | - checkLoadJwtFromLength(); |
| 72 | + if (message.type === 'cookies') { |
| 73 | + elements.forEach(e => cookiesOptGroup.appendChild(e)); |
| 74 | + } else { |
| 75 | + elements.forEach(e => webStorageOptGroup.appendChild(e)); |
66 | 76 | } |
67 | 77 |
|
68 | | - chrome.runtime.onMessage.addListener(jwtMessage); |
| 78 | + updateOptGroups(); |
| 79 | +} |
69 | 80 |
|
70 | | - chrome.tabs.executeScript({ |
71 | | - file: 'js/webstorage.js', |
72 | | - runAt: "document_idle" |
| 81 | +function saveCookie(url, cookie, oldCookie) { |
| 82 | + // Some cookies get duplicated otherwise (chrome.cookies.set bug?) |
| 83 | + chrome.cookies.remove({ |
| 84 | + url: url, |
| 85 | + name: oldCookie.name, |
| 86 | + storeId: oldCookie.storeId |
| 87 | + }); |
| 88 | + chrome.cookies.set({ |
| 89 | + url: url, |
| 90 | + name: oldCookie.name, |
| 91 | + value: cookie.value, |
| 92 | + domain: oldCookie.domain, |
| 93 | + path: oldCookie.path, |
| 94 | + secure: oldCookie.secure, |
| 95 | + httpOnly: oldCookie.httpOnly, |
| 96 | + expirationDate: oldCookie.expirationDate, |
| 97 | + storeId: oldCookie.storeId |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +function saveBackClick() { |
| 102 | + const selected = storageSelect.options[storageSelect.selectedIndex]; |
| 103 | + const type = selected.getAttribute('data-type'); |
| 104 | + const name = selected.text; |
| 105 | + const value = getTokenEditorValue().token; |
| 106 | + |
| 107 | + selected.value = value; |
| 108 | + |
| 109 | + chrome.tabs.query({ active: true, currentWindow: true }, tabs => { |
| 110 | + const data = { |
| 111 | + type: type + 'Save', |
| 112 | + name: name, |
| 113 | + value: value |
| 114 | + }; |
| 115 | + if(type === 'cookie') { |
| 116 | + saveCookie(tabs[0].url, data, |
| 117 | + JSON.parse(selected.getAttribute('data-cookie'))); |
| 118 | + } else { |
| 119 | + chrome.tabs.sendMessage(tabs[0].id, data); |
| 120 | + } |
73 | 121 | }); |
| 122 | +} |
| 123 | + |
| 124 | +function storedJwtSelect() { |
| 125 | + const selected = storageSelect.options[storageSelect.selectedIndex]; |
74 | 126 |
|
75 | | - chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { |
76 | | - chrome.cookies.getAll({ |
77 | | - url: tabs[0].url, |
78 | | - }, function(cookies) { |
79 | | - var result = cookies.map(function(cookie) { |
80 | | - return { |
81 | | - name: cookie.name, |
82 | | - value: cookie.value, |
83 | | - type: 'cookie', |
84 | | - cookie: cookie |
85 | | - } |
86 | | - }); |
87 | | - |
88 | | - jwtMessage({ |
89 | | - type: 'cookies', |
90 | | - tokens: result |
91 | | - }); |
| 127 | + if(selected.getAttribute('name') === '0') { // "None" selected |
| 128 | + saveBackElement.classList.add('hide'); |
| 129 | + return; |
| 130 | + } |
| 131 | + saveBackElement.classList.remove('hide'); |
| 132 | + |
| 133 | + const type = selected.parentElement.getAttribute('label').toLowerCase(); |
| 134 | + |
| 135 | + const name = selected.text; |
| 136 | + const value = selected.value; |
| 137 | + |
| 138 | + setTokenEditorValue(value); |
| 139 | + |
| 140 | + saveBackLink.firstChild.textContent = strings.extension.saveBackTo + type; |
| 141 | +} |
| 142 | + |
| 143 | +function setupListeners() { |
| 144 | + saveBackElement.addEventListener('click', saveBackClick); |
| 145 | + storageSelect.addEventListener('change', storedJwtSelect); |
| 146 | +} |
| 147 | + |
| 148 | +function getCookies() { |
| 149 | + chrome.tabs.query({ active: true, currentWindow: true }, tabs => { |
| 150 | + chrome.cookies.getAll({ |
| 151 | + url: tabs[0].url, |
| 152 | + }, cookies => { |
| 153 | + const result = cookies.map(cookie => { |
| 154 | + return { |
| 155 | + name: cookie.name, |
| 156 | + value: cookie.value, |
| 157 | + type: 'cookie', |
| 158 | + cookie: cookie |
| 159 | + } |
| 160 | + }); |
| 161 | + |
| 162 | + messageHandler({ |
| 163 | + type: 'cookies', |
| 164 | + tokens: result |
92 | 165 | }); |
| 166 | + }); |
93 | 167 | }); |
| 168 | +} |
| 169 | + |
| 170 | +function setupInjectedCode() { |
| 171 | + chrome.runtime.onMessage.addListener(messageHandler); |
94 | 172 |
|
95 | | - checkLoadJwtFromLength(); |
| 173 | + chrome.tabs.executeScript({ |
| 174 | + file: 'js/webstorage.js', |
| 175 | + runAt: "document_idle" |
| 176 | + }); |
96 | 177 | } |
97 | 178 |
|
98 | 179 | export function setupTokenPageInspector() { |
99 | | - |
| 180 | + setupInjectedCode(); |
| 181 | + getCookies(); |
| 182 | + updateOptGroups(); |
| 183 | + setupListeners(); |
100 | 184 | } |
0 commit comments