|
| 1 | +// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@JS() |
| 6 | +library popup; |
| 7 | + |
| 8 | +import 'dart:async'; |
| 9 | +import 'dart:convert'; |
| 10 | +import 'dart:html'; |
| 11 | + |
| 12 | +import 'package:dwds/data/debug_info.dart'; |
| 13 | +import 'package:js/js.dart'; |
| 14 | + |
| 15 | +import 'data_serializers.dart'; |
| 16 | +import 'data_types.dart'; |
| 17 | +import 'messaging.dart'; |
| 18 | +import 'storage.dart'; |
| 19 | +import 'utils.dart'; |
| 20 | + |
| 21 | +const _appIdContainerId = 'appIdContainer'; |
| 22 | +const _appIdDividerId = 'appIdDivider'; |
| 23 | +const _appIdSpanId = 'appId'; |
| 24 | +const _copyIdButtonId = 'copyIdButton'; |
| 25 | +const _copiedSuccessId = 'copiedSuccess'; |
| 26 | +const _fileBugButtonId = 'fileBugButton'; |
| 27 | +const _hiddenClass = 'hidden'; |
| 28 | +const _launchDevToolsButtonId = 'launchDevToolsButton'; |
| 29 | +const _loadingSpinnerId = 'loadingSpinner'; |
| 30 | +const _windowOption = 'windowOption'; |
| 31 | +const _tabOption = 'tabOption'; |
| 32 | + |
| 33 | +Future<int?> get _tabId async { |
| 34 | + final tab = await activeTab; |
| 35 | + return tab?.id; |
| 36 | +} |
| 37 | + |
| 38 | +String? _appId; |
| 39 | + |
| 40 | +Future<void> main() async { |
| 41 | + _registerListeners(); |
| 42 | + await _loadUiAndHideSpinner(); |
| 43 | +} |
| 44 | + |
| 45 | +void _registerListeners() { |
| 46 | + final launchDevToolsButton = |
| 47 | + document.getElementById(_launchDevToolsButtonId) as ButtonElement; |
| 48 | + launchDevToolsButton.addEventListener('click', _launchDevTools); |
| 49 | + |
| 50 | + final copyButton = document.getElementById(_copyIdButtonId) as ButtonElement; |
| 51 | + copyButton.addEventListener('click', _copyAppId); |
| 52 | + |
| 53 | + final fileABugButton = |
| 54 | + document.getElementById(_fileBugButtonId) as ButtonElement; |
| 55 | + fileABugButton.addEventListener('click', _openIssueTracker); |
| 56 | + |
| 57 | + document.addEventListener('DOMContentLoaded', _updateSettingsFromStorage); |
| 58 | + |
| 59 | + final windowOption = document.getElementById(_windowOption) as InputElement; |
| 60 | + windowOption.addEventListener('change', (Event _) { |
| 61 | + _saveSettingsToStorage(windowOption.value); |
| 62 | + }); |
| 63 | + |
| 64 | + final tabOption = document.getElementById(_tabOption) as InputElement; |
| 65 | + tabOption.addEventListener('change', (Event _) { |
| 66 | + _saveSettingsToStorage(tabOption.value); |
| 67 | + }); |
| 68 | +} |
| 69 | + |
| 70 | +Future<void> _loadUiAndHideSpinner() async { |
| 71 | + final inserted = await _insertAppId(); |
| 72 | + if (inserted) { |
| 73 | + _updateElementVisibility(_appIdContainerId, visible: true); |
| 74 | + _updateElementVisibility(_appIdDividerId, visible: true); |
| 75 | + } |
| 76 | + _updateElementVisibility(_loadingSpinnerId, visible: false); |
| 77 | +} |
| 78 | + |
| 79 | +Future<DebugInfo?> _fetchDebugInfo(int? tabId) async { |
| 80 | + if (tabId == null) return null; |
| 81 | + final debugInfo = await fetchStorageObject<DebugInfo>( |
| 82 | + type: StorageObject.debugInfo, |
| 83 | + tabId: tabId, |
| 84 | + ); |
| 85 | + return debugInfo; |
| 86 | +} |
| 87 | + |
| 88 | +Future<bool> _insertAppId() async { |
| 89 | + final tabId = await _tabId; |
| 90 | + final debugInfo = await _fetchDebugInfo(tabId); |
| 91 | + if (debugInfo == null) return false; |
| 92 | + final isInternalBuild = debugInfo.isInternalBuild ?? false; |
| 93 | + final workspaceName = debugInfo.workspaceName; |
| 94 | + if (isInternalBuild && workspaceName != null) { |
| 95 | + _appId = '$workspaceName-$tabId'; |
| 96 | + final appIdSpan = document.getElementById(_appIdSpanId) as SpanElement; |
| 97 | + appIdSpan.setInnerHtml(_appId); |
| 98 | + return true; |
| 99 | + } |
| 100 | + return false; |
| 101 | +} |
| 102 | + |
| 103 | +Future<void> _openIssueTracker(Event _) async { |
| 104 | + final debugInfo = await _fetchDebugInfo(await _tabId); |
| 105 | + final isInternalBuild = debugInfo?.isInternalBuild ?? false; |
| 106 | + final issueTrackerLink = isInternalBuild |
| 107 | + ? 'http://b/issues/new?component=775375&template=1791321' |
| 108 | + : 'https://github.com/dart-lang/webdev/issues/new?labels=dart-debug-extension&projects=&template=dart_debug_extension.md'; |
| 109 | + await createTab(issueTrackerLink); |
| 110 | +} |
| 111 | + |
| 112 | +Future<void> _launchDevTools(Event _) async { |
| 113 | + final tabId = await _tabId; |
| 114 | + final json = jsonEncode( |
| 115 | + serializers.serialize( |
| 116 | + DebugStateChange( |
| 117 | + (b) => b |
| 118 | + ..tabId = tabId |
| 119 | + ..newState = DebugStateChange.startDebugging, |
| 120 | + ), |
| 121 | + ), |
| 122 | + ); |
| 123 | + await sendRuntimeMessage( |
| 124 | + type: MessageType.debugStateChange, |
| 125 | + body: json, |
| 126 | + sender: Script.popup, // change to popup |
| 127 | + recipient: Script.background, |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +void _copyAppId(Event _) { |
| 132 | + if (_appId == null) return; |
| 133 | + final clipboard = window.navigator.clipboard; |
| 134 | + if (clipboard == null) return; |
| 135 | + clipboard.writeText(_appId!); |
| 136 | + _updateElementVisibility(_copiedSuccessId, visible: true); |
| 137 | +} |
| 138 | + |
| 139 | +Future<void> _updateSettingsFromStorage(Event _) async { |
| 140 | + final devToolsOpener = await fetchStorageObject<DevToolsOpener>( |
| 141 | + type: StorageObject.devToolsOpener, |
| 142 | + ); |
| 143 | + final openInNewWindow = devToolsOpener?.newWindow ?? false; |
| 144 | + _getRadioButton(_windowOption).checked = openInNewWindow; |
| 145 | + _getRadioButton(_tabOption).checked = !openInNewWindow; |
| 146 | +} |
| 147 | + |
| 148 | +Future<void> _saveSettingsToStorage(String? devToolsOpener) async { |
| 149 | + if (devToolsOpener == null) return; |
| 150 | + await setStorageObject<DevToolsOpener>( |
| 151 | + type: StorageObject.devToolsOpener, |
| 152 | + value: DevToolsOpener( |
| 153 | + (b) => b..newWindow = devToolsOpener == 'window', |
| 154 | + ), |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +RadioButtonInputElement _getRadioButton(String id) { |
| 159 | + return document.getElementById(id) as RadioButtonInputElement; |
| 160 | +} |
| 161 | + |
| 162 | +void _updateElementVisibility(String elementId, {required bool visible}) { |
| 163 | + final element = document.getElementById(elementId); |
| 164 | + if (element == null) return; |
| 165 | + if (visible) { |
| 166 | + element.classes.remove(_hiddenClass); |
| 167 | + } else { |
| 168 | + element.classes.add(_hiddenClass); |
| 169 | + } |
| 170 | +} |
0 commit comments