Adrià Vilanova MartÃnez | 5a8055b | 2022-09-29 13:05:19 +0200 | [diff] [blame] | 1 | import {MDCTooltip} from '@material/tooltip'; |
| 2 | import {waitFor} from 'poll-until-promise'; |
| 3 | |
| 4 | import {parseUrl} from '../../common/commonUtils.js'; |
| 5 | import {injectStylesheet} from '../../common/contentScriptsUtils.js'; |
| 6 | import {getDocURL} from '../../common/extUtils.js'; |
| 7 | import {getOptions} from '../../common/optionsUtils.js'; |
| 8 | |
| 9 | import {createExtBadge} from './utils/common.js'; |
| 10 | |
| 11 | // Forums where Nested Replies have been enabled |
| 12 | const NRS_ENABLED_FORUM_IDS = [51488989]; |
| 13 | |
| 14 | export default class ThreadPageDesignWarning { |
| 15 | constructor() { |
| 16 | // We have to save whether the old UI was enabled at startup, since that's |
| 17 | // the moment when it takes effect. If the option changes while the tab is |
| 18 | // open, it won't take effect. |
| 19 | getOptions([ |
| 20 | 'interopthreadpage', 'interopthreadpage_mode' |
| 21 | ]).then(options => { |
| 22 | this.shouldShowWarning = options.interopthreadpage && |
| 23 | options.interopthreadpage_mode == 'previous'; |
| 24 | |
| 25 | if (this.shouldShowWarning) { |
| 26 | injectStylesheet( |
| 27 | chrome.runtime.getURL('css/thread_page_design_warning.css')); |
| 28 | } |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | injectWarning(content) { |
| 33 | let div = document.createElement('div'); |
| 34 | div.classList.add('TWPT-warning'); |
| 35 | |
| 36 | let icon = document.createElement('material-icon'); |
| 37 | icon.classList.add('TWPT-warning--icon'); |
| 38 | |
| 39 | let iconContent = document.createElement('i'); |
| 40 | iconContent.classList.add('material-icon-i', 'material-icons-extended'); |
| 41 | iconContent.setAttribute('role', 'img'); |
| 42 | iconContent.setAttribute('aria-hidden', 'true'); |
| 43 | iconContent.textContent = 'warning'; |
| 44 | |
| 45 | icon.append(iconContent); |
| 46 | |
| 47 | let text = document.createElement('div'); |
| 48 | text.classList.add('TWPT-warning--text'); |
| 49 | text.textContent = |
| 50 | chrome.i18n.getMessage('inject_threadpagedesign_warning'); |
| 51 | |
| 52 | let btn = document.createElement('a'); |
| 53 | btn.classList.add('TWPT-warning--btn'); |
| 54 | btn.href = |
| 55 | getDocURL('features.md#Thread-page-design-in-the-Community-Console'); |
| 56 | btn.setAttribute('target', '_blank'); |
| 57 | btn.setAttribute('rel', 'noopener noreferrer'); |
| 58 | |
| 59 | const [badge, badgeTooltip] = createExtBadge(); |
| 60 | |
| 61 | let btnText = document.createElement('div'); |
| 62 | btnText.textContent = chrome.i18n.getMessage('btn_learnmore'); |
| 63 | |
| 64 | btn.append(badge, btnText); |
| 65 | |
| 66 | div.append(icon, text, btn); |
| 67 | content.prepend(div); |
| 68 | |
| 69 | new MDCTooltip(badgeTooltip); |
| 70 | } |
| 71 | |
| 72 | injectWarningIfApplicable(content) { |
| 73 | return waitFor( |
| 74 | () => { |
| 75 | if (this.shouldShowWarning === undefined) |
| 76 | return Promise.reject( |
| 77 | new Error('shouldShowWarning is not defined.')); |
| 78 | |
| 79 | return Promise.resolve(this.shouldShowWarning); |
| 80 | }, |
| 81 | { |
| 82 | interval: 500, |
| 83 | timeout: 10 * 1000, |
| 84 | }) |
| 85 | .then(shouldShowWarning => { |
| 86 | if (!shouldShowWarning) return; |
| 87 | |
| 88 | let thread = parseUrl(location.href); |
| 89 | if (thread === false) |
| 90 | throw new Error('current thread cannot be parsed.'); |
| 91 | |
| 92 | if (NRS_ENABLED_FORUM_IDS.includes(parseInt(thread.forum))) |
| 93 | this.injectWarning(content); |
| 94 | }) |
| 95 | .catch(err => { |
| 96 | console.error( |
| 97 | '[threadPageDesignWarning] An error ocurred while trying to decide whether to show warning: ', |
| 98 | err); |
| 99 | }); |
| 100 | } |
| 101 | } |