Adrià Vilanova MartÃnez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 1 | // In order to pass i18n strings and settings values to the injected scripts, |
| 2 | // which don't have access to the chrome.* APIs, we use event listeners. |
| 3 | |
| 4 | export function setUpListener() { |
| 5 | chrome.storage.sync.get(null, function(options) { |
| 6 | window.addEventListener('TWPT_sendRequest', evt => { |
| 7 | var request = evt.detail; |
| 8 | switch (request.data.action) { |
| 9 | case 'geti18nMessage': |
| 10 | var data = chrome.i18n.getMessage( |
| 11 | request.data.msg, |
| 12 | (Array.isArray(request.data.placeholders) ? |
| 13 | request.data.placeholders : |
| 14 | [])); |
| 15 | break; |
| 16 | |
| 17 | case 'getProfileIndicatorOptions': |
| 18 | var data = { |
| 19 | 'indicatorDot': options.profileindicator, |
| 20 | 'numPosts': options.profileindicatoralt |
| 21 | }; |
| 22 | break; |
| 23 | |
| 24 | case 'getNumPostMonths': |
| 25 | var data = options.profileindicatoralt_months; |
| 26 | break; |
| 27 | |
| 28 | default: |
| 29 | var data = 'unknownAction'; |
| 30 | console.warn('Unknown action ' + request.data.action + '.'); |
| 31 | break; |
| 32 | } |
| 33 | |
| 34 | var response = { |
| 35 | data, |
| 36 | requestId: request.id, |
| 37 | prefix: (request.prefix || 'TWPT'), |
| 38 | }; |
| 39 | |
| 40 | window.postMessage(response, '*'); |
| 41 | }); |
| 42 | }); |
| 43 | } |