Pop-up dictionary browser extension for language learning. Successor to Yomichan. (PERSONAL FORK)
at lambda-fork/main 264 lines 6.9 kB view raw
1/* 2 * Copyright (C) 2023-2025 Yomitan Authors 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18import type {CrossFrameAPIPort} from '../../ext/js/comm/cross-frame-api.js'; 19import type {Response, Timeout, TokenString} from './core'; 20import type {ModifierKey} from './input'; 21import type {ContentDetails as PopupContentDetails, ValidSize} from './popup'; 22import type {GetOrCreatePopupDetails} from './popup-factory'; 23import type {OptionsContext} from './settings'; 24import type { 25 ApiMap as BaseApiMap, 26 ApiParams as BaseApiParams, 27 ApiNames as BaseApiNames, 28 ApiMapInit as BaseApiMapInit, 29 ApiHandler as BaseApiHandler, 30 ApiReturn as BaseApiReturn, 31 ApiReturnAny as BaseApiReturnAny, 32} from './api-map'; 33import type { 34 DirectApiFrameClientMessageAny as DisplayDirectApiFrameClientMessageAny, 35 DirectApiMessageAny as DisplayDirectApiMessageAny, 36 DirectApiReturnAny as DisplayDirectApiReturnAny, 37 ContentDetails as DisplayContentDetails, 38} from './display'; 39import type {ChildFrameRect} from 'frame-offset-forwarder'; 40import type {RequestFrameInfoResponseParams, RequestFrameInfoResponseReturn} from './frame-ancestry-handler'; 41 42export type CrossFrameAPIPortEvents = { 43 disconnect: CrossFrameAPIPort; 44}; 45 46export type AcknowledgeMessage = { 47 type: 'ack'; 48 id: number; 49}; 50 51export type ResultMessage = { 52 type: 'result'; 53 id: number; 54 data: Response<ApiReturnAny>; 55}; 56 57export type InvokeMessage = { 58 type: 'invoke'; 59 id: number; 60 data: ApiMessageAny; 61}; 62 63export type Message = AcknowledgeMessage | ResultMessage | InvokeMessage; 64 65export type Invocation = { 66 id: number; 67 resolve: (value: ApiReturnAny) => void; 68 reject: (reason: Error) => void; 69 responseTimeout: number; 70 action: string; 71 ack: boolean; 72 timer: Timeout | null; 73}; 74 75export type PortDetails = CrossFrameCommunicationPortDetails; 76 77export type CrossFrameCommunicationPortDetails = { 78 name: 'cross-frame-communication-port'; 79 otherTabId: number; 80 otherFrameId: number; 81}; 82 83export type ApiSurface = { 84 displayPopupMessage1: { 85 params: DisplayDirectApiFrameClientMessageAny; 86 return: DisplayDirectApiReturnAny; 87 }; 88 displayPopupMessage2: { 89 params: DisplayDirectApiMessageAny; 90 return: DisplayDirectApiReturnAny; 91 }; 92 frontendClosePopup: { 93 params: void; 94 return: void; 95 }; 96 frontendCopySelection: { 97 params: void; 98 return: void; 99 }; 100 frontendGetPopupSelectionText: { 101 params: void; 102 return: string; 103 }; 104 frontendGetPopupInfo: { 105 params: void; 106 return: { 107 popupId: string | null; 108 }; 109 }; 110 frontendGetPageInfo: { 111 params: void; 112 return: { 113 url: string; 114 documentTitle: string; 115 }; 116 }; 117 frameOffsetForwarderGetChildFrameRect: { 118 params: { 119 frameId: number; 120 }; 121 return: ChildFrameRect | null; 122 }; 123 hotkeyHandlerForwardHotkey: { 124 params: { 125 key: string; 126 modifiers: ModifierKey[]; 127 }; 128 return: boolean; 129 }; 130 popupFactoryGetOrCreatePopup: { 131 params: GetOrCreatePopupDetails; 132 return: {id: string, depth: number, frameId: number}; 133 }; 134 popupFactorySetOptionsContext: { 135 params: { 136 id: string; 137 optionsContext: OptionsContext; 138 }; 139 return: void; 140 }; 141 popupFactoryHide: { 142 params: { 143 id: string; 144 changeFocus: boolean; 145 }; 146 return: void; 147 }; 148 popupFactoryIsVisible: { 149 params: { 150 id: string; 151 }; 152 return: boolean; 153 }; 154 popupFactorySetVisibleOverride: { 155 params: { 156 id: string; 157 value: boolean; 158 priority: number; 159 }; 160 return: TokenString | null; 161 }; 162 popupFactoryClearVisibleOverride: { 163 params: { 164 id: string; 165 token: TokenString; 166 }; 167 return: boolean; 168 }; 169 popupFactoryContainsPoint: { 170 params: { 171 id: string; 172 x: number; 173 y: number; 174 }; 175 return: boolean; 176 }; 177 popupFactoryShowContent: { 178 params: { 179 id: string; 180 details: PopupContentDetails; 181 displayDetails: DisplayContentDetails | null; 182 }; 183 return: void; 184 }; 185 popupFactorySetCustomCss: { 186 params: { 187 id: string; 188 css: string; 189 }; 190 return: void; 191 }; 192 popupFactoryClearAutoPlayTimer: { 193 params: { 194 id: string; 195 }; 196 return: void; 197 }; 198 popupFactorySetContentScale: { 199 params: { 200 id: string; 201 scale: number; 202 }; 203 return: void; 204 }; 205 popupFactoryUpdateTheme: { 206 params: { 207 id: string; 208 }; 209 return: void; 210 }; 211 popupFactorySetCustomOuterCss: { 212 params: { 213 id: string; 214 css: string; 215 useWebExtensionApi: boolean; 216 }; 217 return: void; 218 }; 219 popupFactoryGetFrameSize: { 220 params: { 221 id: string; 222 }; 223 return: ValidSize; 224 }; 225 popupFactorySetFrameSize: { 226 params: { 227 id: string; 228 width: number; 229 height: number; 230 }; 231 return: boolean; 232 }; 233 frameAncestryHandlerRequestFrameInfoResponse: { 234 params: RequestFrameInfoResponseParams; 235 return: RequestFrameInfoResponseReturn; 236 }; 237 popupFactoryIsPointerOver: { 238 params: { 239 id: string; 240 }; 241 return: boolean; 242 }; 243}; 244 245export type ApiNames = BaseApiNames<ApiSurface>; 246 247export type ApiMapInit = BaseApiMapInit<ApiSurface>; 248 249export type ApiMap = BaseApiMap<ApiSurface, []>; 250 251export type ApiHandler<TName extends ApiNames> = BaseApiHandler<ApiSurface[TName]>; 252 253export type ApiParams<TName extends ApiNames> = BaseApiParams<ApiSurface[TName]>; 254 255export type ApiReturn<TName extends ApiNames> = BaseApiReturn<ApiSurface[TName]>; 256 257export type ApiReturnAny = BaseApiReturnAny<ApiSurface>; 258 259export type ApiMessageAny = {[name in ApiNames]: ApiMessage<name>}[ApiNames]; 260 261type ApiMessage<TName extends ApiNames> = { 262 action: TName; 263 params: ApiParams<TName>; 264};