1# AccessibilityExtensionContext (Accessibility Extension Context) 2 3The **AccessibilityExtensionContext** module, inherited from **ExtensionContext**, provides context for **AccessibilityExtensionAbility**. 4 5You can use the APIs of this module to configure the concerned information, obtain root information, and inject gestures. 6 7> **NOTE** 8> 9> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> 11> The APIs of this module can be used only in the stage model. 12 13## Usage 14 15Before using the **AccessibilityExtensionContext** module, you must define a child class that inherits from **AccessibilityExtensionAbility**. 16 17```ts 18import AccessibilityExtensionAbility, { 19 AccessibilityExtensionContext, 20} from '@ohos.application.AccessibilityExtensionAbility'; 21 22let axContext: AccessibilityExtensionContext; 23 24class EntryAbility extends AccessibilityExtensionAbility { 25 axContext = this.context; 26} 27``` 28 29## FocusDirection 30 31Enumerates the focus directions. 32 33**System capability**: SystemCapability.BarrierFree.Accessibility.Core 34 35| Name | Description | 36| -------- | ------- | 37| up | Search for the next focusable item above the current item in focus.| 38| down | Search for the next focusable item below the current item in focus.| 39| left | Search for the next focusable item on the left of the current item in focus.| 40| right | Search for the next focusable item on the right of the current item in focus.| 41| forward | Search for the next focusable item before the current item in focus.| 42| backward | Search for the next focusable item after the current item in focus.| 43 44## FocusType 45 46Enumerates the focus types. 47 48**System capability**: SystemCapability.BarrierFree.Accessibility.Core 49 50| Name | Description | 51| ------------- | ----------- | 52| accessibility | Accessibility focus.| 53| normal | Normal focus. | 54 55## Rect 56 57Defines a rectangle. 58 59**System capability**: SystemCapability.BarrierFree.Accessibility.Core 60 61| Name | Type | Readable | Writable | Description | 62| ------ | ------ | ---- | ---- | --------- | 63| left | number | Yes | No | Left boundary of the rectangle.| 64| top | number | Yes | No | Top boundary of the rectangle.| 65| width | number | Yes | No | Width of the rectangle. | 66| height | number | Yes | No | Height of the rectangle. | 67 68## WindowType 69 70Enumerates the window types. 71 72**System capability**: SystemCapability.BarrierFree.Accessibility.Core 73 74| Name | Description | 75| ----------- | --------- | 76| application | Application window.| 77| system | System window.| 78 79## AccessibilityExtensionContext.setTargetBundleName 80 81setTargetBundleName(targetNames: Array\<string>): Promise\<void>; 82 83Sets the concerned target bundle. This API uses a promise to return the result. 84 85**System capability**: SystemCapability.BarrierFree.Accessibility.Core 86 87**Parameters** 88 89| Name | Type | Mandatory | Description | 90| ----------- | ------------------- | ---- | -------- | 91| targetNames | Array<string> | Yes | Name of the target bundle.| 92 93**Return value** 94 95| Type | Description | 96| ------------------- | ---------------- | 97| Promise<void> | Promise that returns no value.| 98 99**Example** 100 101```ts 102let targetNames = ['com.ohos.xyz']; 103 104try { 105 axContext.setTargetBundleName(targetNames).then(() => { 106 console.info('set target bundle names success'); 107 }).catch((err: object) => { 108 console.error(`failed to set target bundle names, because ${JSON.stringify(err)}`); 109 }); 110} catch (exception) { 111 console.error(`failed to set target bundle names, because ${JSON.stringify(exception)}`); 112} 113``` 114 115## AccessibilityExtensionContext.setTargetBundleName 116 117setTargetBundleName(targetNames: Array\<string>, callback: AsyncCallback\<void>): void; 118 119Sets the concerned target bundle. This API uses an asynchronous callback to return the result. 120 121**System capability**: SystemCapability.BarrierFree.Accessibility.Core 122 123**Parameters** 124 125| Name | Type | Mandatory | Description | 126| ----------- | ------------------------- | ---- | ---------------------------------------- | 127| targetNames | Array<string> | Yes | Name of the target bundle. | 128| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation fails, **error** that contains data is returned.| 129 130**Example** 131 132```ts 133import { BusinessError } from '@ohos.base'; 134 135let targetNames = ['com.ohos.xyz']; 136try { 137 axContext.setTargetBundleName(targetNames, (err: BusinessError<void>) => { 138 if (err) { 139 console.error(`failed to set target bundle names, because ${JSON.stringify(err)}`); 140 return; 141 } 142 console.info('set target bundle names success'); 143 }); 144} catch (exception) { 145 console.error(`failed to set target bundle names, because ${JSON.stringify(exception)}`); 146} 147``` 148 149## AccessibilityExtensionContext.getFocusElement 150 151getFocusElement(isAccessibilityFocus?: boolean): Promise\<AccessibilityElement>; 152 153Obtains the focus element. This API uses a promise to return the result. 154 155**System capability**: SystemCapability.BarrierFree.Accessibility.Core 156 157**Parameters** 158 159| Name | Type | Mandatory | Description | 160| -------------------- | ------- | ---- | ------------------- | 161| isAccessibilityFocus | boolean | No | Whether the obtained focus element is an accessibility focus. The default value is **false**.| 162 163**Return value** 164 165| Type | Description | 166| ----------------------------------- | ---------------------- | 167| Promise<AccessibilityElement> | Promise used to return the current focus element.| 168 169**Error codes** 170 171For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 172 173| ID | Error Message | 174| ------- | ---------------------------------------- | 175| 9300003 | Do not have accessibility right for this operation. | 176 177**Example** 178 179```ts 180import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 181 182let focusElement: AccessibilityElement; 183try { 184 axContext.getFocusElement().then((data: AccessibilityElement) => { 185 focusElement = data; 186 console.log('get focus element success'); 187 }).catch((err: object) => { 188 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 189 }); 190} catch (exception) { 191 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 192} 193``` 194 195## AccessibilityExtensionContext.getFocusElement 196 197getFocusElement(callback: AsyncCallback\<AccessibilityElement>): void; 198 199Obtains the focus element. This API uses an asynchronous callback to return the result. 200 201**System capability**: SystemCapability.BarrierFree.Accessibility.Core 202 203**Parameters** 204 205| Name | Type | Mandatory | Description | 206| -------- | ---------------------------------------- | ---- | ----------------- | 207| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the current focus element.| 208 209**Error codes** 210 211For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 212 213| ID | Error Message | 214| ------- | ---------------------------------------- | 215| 9300003 | Do not have accessibility right for this operation. | 216 217**Example** 218 219```ts 220import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 221import { BusinessError } from '@ohos.base'; 222 223let focusElement: AccessibilityElement; 224try { 225 axContext.getFocusElement((err: BusinessError<void>, data: AccessibilityElement) => { 226 if (err) { 227 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 228 return; 229 } 230 focusElement = data; 231 console.info('get focus element success'); 232 }); 233} catch (exception) { 234 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 235} 236``` 237 238## AccessibilityExtensionContext.getFocusElement 239 240getFocusElement(isAccessibilityFocus: boolean, callback: AsyncCallback\<AccessibilityElement>): void; 241 242Obtains the focus element. This API uses an asynchronous callback to return the result. 243 244**System capability**: SystemCapability.BarrierFree.Accessibility.Core 245 246**Parameters** 247 248| Name | Type | Mandatory | Description | 249| -------------------- | ---------------------------------------- | ---- | ----------------- | 250| isAccessibilityFocus | boolean | Yes | Whether the obtained focus element is an accessibility focus. | 251| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the current focus element.| 252 253**Error codes** 254 255For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 256 257| ID | Error Message | 258| ------- | ---------------------------------------- | 259| 9300003 | Do not have accessibility right for this operation. | 260 261**Example** 262 263```ts 264import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 265import { BusinessError } from '@ohos.base'; 266 267let focusElement: AccessibilityElement; 268let isAccessibilityFocus = true; 269 270try { 271 axContext.getFocusElement(isAccessibilityFocus, (err: BusinessError<void>, data: AccessibilityElement) => { 272 if (err) { 273 console.error(`failed to get focus element, because ${JSON.stringify(err)}`); 274 return; 275 } 276 focusElement = data; 277 console.info('get focus element success'); 278 }); 279} catch (exception) { 280 console.error(`failed to get focus element, because ${JSON.stringify(exception)}`); 281} 282``` 283## AccessibilityExtensionContext.getWindowRootElement 284 285getWindowRootElement(windowId?: number): Promise\<AccessibilityElement>; 286 287Obtains the root element of a window. This API uses a promise to return the result. 288 289**System capability**: SystemCapability.BarrierFree.Accessibility.Core 290 291**Parameters** 292 293| Name | Type | Mandatory | Description | 294| -------- | ------ | ---- | ---------------------- | 295| windowId | number | No | Window for which you want to obtain the root element. If this parameter is not specified, it indicates the current active window.| 296 297**Return value** 298 299| Type | Description | 300| ----------------------------------- | ---------------------- | 301| Promise<AccessibilityElement> | Promise used to return the root element of the specified window.| 302 303**Error codes** 304 305For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 306 307| ID | Error Message | 308| ------- | ---------------------------------------- | 309| 9300003 | Do not have accessibility right for this operation. | 310 311**Example** 312 313```ts 314import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 315 316let rootElement: AccessibilityElement; 317try { 318 axContext.getWindowRootElement().then((data: AccessibilityElement) => { 319 rootElement = data; 320 console.log('get root element of the window success'); 321 }).catch((err: object) => { 322 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 323 }); 324} catch (exception) { 325 console.error(`failed to get root element of the window, ${JSON.stringify(exception)}`); 326} 327``` 328 329## AccessibilityExtensionContext.getWindowRootElement 330 331getWindowRootElement(callback: AsyncCallback\<AccessibilityElement>): void; 332 333Obtains the root element of a window. This API uses an asynchronous callback to return the result. 334 335**System capability**: SystemCapability.BarrierFree.Accessibility.Core 336 337**Parameters** 338 339| Name | Type | Mandatory | Description | 340| -------- | ---------------------------------------- | ---- | ------------------ | 341| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the root element.| 342 343**Error codes** 344 345For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 346 347| ID | Error Message | 348| ------- | ---------------------------------------- | 349| 9300003 | Do not have accessibility right for this operation. | 350 351**Example** 352 353```ts 354import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 355import { BusinessError } from '@ohos.base'; 356 357let rootElement: AccessibilityElement; 358try { 359 axContext.getWindowRootElement((err: BusinessError<void> 360 , data: AccessibilityElement) => { 361 if (err) { 362 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 363 return; 364 } 365 rootElement = data; 366 console.info('get root element of the window success'); 367 }); 368} catch (exception) { 369 console.error(`failed to get root element of the window, because ${JSON.stringify(exception)}`); 370} 371``` 372 373## AccessibilityExtensionContext.getWindowRootElement 374 375getWindowRootElement(windowId: number, callback: AsyncCallback\<AccessibilityElement>): void; 376 377Obtains the root element of a window. This API uses an asynchronous callback to return the result. 378 379**System capability**: SystemCapability.BarrierFree.Accessibility.Core 380 381**Parameters** 382 383| Name | Type | Mandatory | Description | 384| -------- | ---------------------------------------- | ---- | ---------------------- | 385| windowId | number | Yes | Window for which you want to obtain the root element. If this parameter is not specified, it indicates the current active window.| 386| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the root element. | 387 388**Error codes** 389 390For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 391 392| ID | Error Message | 393| ------- | ---------------------------------------- | 394| 9300003 | Do not have accessibility right for this operation. | 395 396**Example** 397 398```ts 399import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 400import { BusinessError } from '@ohos.base'; 401 402let rootElement: AccessibilityElement; 403let windowId = 10; 404 405try { 406 axContext.getWindowRootElement(windowId, (err: BusinessError<void>, data: AccessibilityElement) => { 407 if (err) { 408 console.error(`failed to get root element of the window, because ${JSON.stringify(err)}`); 409 return; 410 } 411 rootElement = data; 412 console.info('get root element of the window success'); 413 }); 414} catch (exception) { 415 console.error(`failed to get root element of the window, because ${JSON.stringify(exception)}`); 416} 417``` 418 419## AccessibilityExtensionContext.getWindows 420 421getWindows(displayId?: number): Promise\<Array\<AccessibilityElement>>; 422 423Obtains the list of windows on a display. This API uses a promise to return the result. 424 425**System capability**: SystemCapability.BarrierFree.Accessibility.Core 426 427**Parameters** 428 429| Name | Type | Mandatory | Description | 430| --------- | ------ | ---- | --------------------- | 431| displayId | number | No | ID of the display from which the window information is obtained. If this parameter is not specified, it indicates the default main display.| 432 433**Return value** 434 435| Type | Description | 436| ---------------------------------------- | ---------------------- | 437| Promise<Array<AccessibilityElement>> | Promise used to return the window list.| 438 439**Error codes** 440 441For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 442 443| ID | Error Message | 444| ------- | ---------------------------------------- | 445| 9300003 | Do not have accessibility right for this operation. | 446 447**Example** 448 449```ts 450import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 451 452let windows: AccessibilityElement[]; 453try { 454 axContext.getWindows().then((data: AccessibilityElement[]) => { 455 windows = data; 456 console.log('get windows success'); 457 }).catch((err: object) => { 458 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 459 }); 460} catch (exception) { 461 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 462} 463``` 464 465## AccessibilityExtensionContext.getWindows 466 467getWindows(callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 468 469Obtains the list of windows on this display. This API uses an asynchronous callback to return the result. 470 471**System capability**: SystemCapability.BarrierFree.Accessibility.Core 472 473**Parameters** 474 475| Name | Type | Mandatory | Description | 476| -------- | ---------------------------------------- | ---- | ----------------- | 477| callback | AsyncCallback<Array<AccessibilityElement>> | Yes | Callback used to return the window list.| 478 479**Error codes** 480 481For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 482 483| ID | Error Message | 484| ------- | ---------------------------------------- | 485| 9300003 | Do not have accessibility right for this operation. | 486 487**Example** 488 489```ts 490import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 491import { BusinessError } from '@ohos.base'; 492 493let windows: AccessibilityElement[]; 494try { 495 axContext.getWindows((err: BusinessError<void>, data: AccessibilityElement[]) => { 496 if (err) { 497 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 498 return; 499 } 500 windows = data; 501 console.info('get windows success'); 502 }); 503} catch (exception) { 504 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 505} 506``` 507 508## AccessibilityExtensionContext.getWindows 509 510getWindows(displayId: number, callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 511 512Obtains the list of windows on a display. This API uses an asynchronous callback to return the result. 513 514**System capability**: SystemCapability.BarrierFree.Accessibility.Core 515 516**Parameters** 517 518| Name | Type | Mandatory | Description | 519| --------- | ---------------------------------------- | ---- | --------------------- | 520| displayId | number | Yes | ID of the display from which the window information is obtained. If this parameter is not specified, it indicates the default main display.| 521| callback | AsyncCallback<Array<AccessibilityElement>> | Yes | Callback used to return the window list. | 522 523**Error codes** 524 525For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 526 527| ID | Error Message | 528| ------- | ---------------------------------------- | 529| 9300003 | Do not have accessibility right for this operation. | 530 531**Example** 532 533```ts 534import { AccessibilityElement } from '@ohos.application.AccessibilityExtensionAbility'; 535import { BusinessError } from '@ohos.base'; 536 537let windows: AccessibilityElement[]; 538let displayId = 10; 539try { 540 axContext.getWindows(displayId, (err: BusinessError<void>, data: AccessibilityElement[]) => { 541 if (err) { 542 console.error(`failed to get windows, because ${JSON.stringify(err)}`); 543 return; 544 } 545 windows = data; 546 console.info('get windows success'); 547 }); 548} catch (exception) { 549 console.error(`failed to get windows, because ${JSON.stringify(exception)}`); 550} 551``` 552 553## AccessibilityExtensionContext.injectGesture 554 555injectGesture(gesturePath: GesturePath): Promise\<void>; 556 557Inject a gesture. This API uses a promise to return the result. 558 559**System capability**: SystemCapability.BarrierFree.Accessibility.Core 560 561**Parameters** 562 563| Name | Type | Mandatory | Description | 564| ----------- | ---------------------------------------- | ---- | ---------- | 565| gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | Yes | Path of the gesture to inject.| 566 567**Return value** 568 569| Type | Description | 570| ------------------- | ---------------- | 571| Promise<void> | Promise that returns no value.| 572 573**Error codes** 574 575For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 576 577| ID | Error Message | 578| ------- | ---------------------------------------- | 579| 9300003 | Do not have accessibility right for this operation. | 580 581**Example** 582 583```ts 584import GesturePath from '@ohos.accessibility.GesturePath'; 585import GesturePoint from '@ohos.accessibility.GesturePoint'; 586 587let gesturePath: GesturePath.GesturePath = new GesturePath.GesturePath(100); 588try { 589 for (let i = 0; i < 10; i++) { 590 let gesturePoint = new GesturePoint.GesturePoint(100, i * 200); 591 gesturePath.points.push(gesturePoint); 592 } 593 axContext.injectGesture(gesturePath).then(() => { 594 console.info('inject gesture success'); 595 }).catch((err: object) => { 596 console.error(`failed to inject gesture, because ${JSON.stringify(err)}`); 597 }); 598} catch (exception) { 599 console.error(`failed to inject gesture, because ${JSON.stringify(exception)}`); 600} 601``` 602## AccessibilityExtensionContext.injectGesture 603 604injectGesture(gesturePath: GesturePath, callback: AsyncCallback\<void>): void 605 606Inject a gesture. This API uses an asynchronous callback to return the result. 607 608**System capability**: SystemCapability.BarrierFree.Accessibility.Core 609 610**Parameters** 611 612| Name | Type | Mandatory | Description | 613| ----------- | ---------------------------------------- | ---- | ------------------- | 614| gesturePath | [GesturePath](js-apis-accessibility-GesturePath.md#gesturepath) | Yes | Path of the gesture to inject. | 615| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 616 617**Error codes** 618 619For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 620 621| ID | Error Message | 622| ------- | ---------------------------------------- | 623| 9300003 | Do not have accessibility right for this operation. | 624 625**Example** 626 627```ts 628import GesturePath from '@ohos.accessibility.GesturePath'; 629import GesturePoint from '@ohos.accessibility.GesturePoint'; 630 631let gesturePath: GesturePath.GesturePath = new GesturePath.GesturePath(100); 632try { 633 for (let i = 0; i < 10; i++) { 634 let gesturePoint = new GesturePoint.GesturePoint(100, i * 200); 635 gesturePath.points.push(gesturePoint); 636 } 637 axContext.injectGesture(gesturePath, (err) => { 638 if (err) { 639 console.error(`failed to inject gesture, because ${JSON.stringify(err)}`); 640 return; 641 } 642 console.info('inject gesture success'); 643 }); 644} catch (exception) { 645 console.error(`failed to inject gesture, because ${JSON.stringify(exception)}`); 646} 647``` 648## AccessibilityElement<sup>9+</sup> 649 650Defines the accessibilityelement. Before calling APIs of **AccessibilityElement**, you must call [AccessibilityExtensionContext.getFocusElement()](#accessibilityextensioncontextgetfocuselement) or [AccessibilityExtensionContext.getWindowRootElement()](#accessibilityextensioncontextgetwindowrootelement) to obtain an **AccessibilityElement** instance. 651 652**System capability**: SystemCapability.BarrierFree.Accessibility.Core 653 654### attributeNames 655 656attributeNames\<T extends keyof ElementAttributeValues>(): Promise\<Array\<T>>; 657 658Obtains all attribute names of this element. This API uses a promise to return the result. 659 660**System capability**: SystemCapability.BarrierFree.Accessibility.Core 661 662**Return value** 663 664| Type | Description | 665| ----------------------------- | ------------------------ | 666| Promise<Array<T>> | Promise used to return all attribute names of the element.| 667 668**Example** 669 670```ts 671import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 672 673let attributeNames: ElementAttributeKeys; 674rootElement.attributeNames().then((data: ElementAttributeKeys) => { 675 console.log('get attribute names success'); 676 attributeNames = data; 677}).catch((err: object) => { 678 console.log(`failed to get attribute names, because ${JSON.stringify(err)}`); 679}); 680``` 681### attributeNames 682 683attributeNames\<T extends keyof ElementAttributeValues>(callback: AsyncCallback\<Array\<T>>): void; 684 685Obtains all attribute names of this element. This API uses an asynchronous callback to return the result. 686 687**System capability**: SystemCapability.BarrierFree.Accessibility.Core 688 689**Parameters** 690 691| Name | Type | Mandatory | Description | 692| -------- | ----------------------------------- | ---- | ------------------- | 693| callback | AsyncCallback<Array<T>> | Yes | Callback used to return all attribute names of the element.| 694 695**Example** 696 697```ts 698import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 699import { BusinessError } from '@ohos.base'; 700 701let attributeNames: ElementAttributeKeys[]; 702rootElement.attributeNames((err: BusinessError<void>, data: ElementAttributeKeys[]) => { 703 if (err) { 704 console.error(`failed to get attribute names, because ${JSON.stringify(err)}`); 705 return; 706 } 707 attributeNames = data; 708 console.info('get attribute names success'); 709}); 710``` 711### attributeValue 712 713attributeValue\<T extends keyof ElementAttributeValues>(attributeName: T): Promise\<ElementAttributeValues[T]>; 714 715Obtains the attribute value based on an attribute name. This API uses a promise to return the result. 716 717**System capability**: SystemCapability.BarrierFree.Accessibility.Core 718 719**Parameters** 720 721| Name | Type | Mandatory | Description | 722| ------------- | ---- | ---- | -------- | 723| attributeName | ElementAttributeKeys | Yes | Attribute name.| 724 725**Return value** 726 727| Type | Description | 728| ---------------------------------------- | --------------------------- | 729| Promise<ElementAttributeValues[T]> | Promise used to return the attribute value.| 730 731**Error codes** 732 733For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 734 735| ID | Error Message | 736| ------- | ----------------------------- | 737| 9300004 | This property does not exist. | 738 739**Example** 740 741```ts 742import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 743 744let attributeName: ElementAttributeKeys = 'bundleName'; 745let attributeValue: string; 746try { 747 rootElement.attributeValue(attributeName).then((data: string) => { 748 console.log('get attribute value by name success'); 749 attributeValue = data; 750 }).catch((err: object) => { 751 console.error(`failed to get attribute value, because ${JSON.stringify(err)}`); 752 }); 753} catch (exception) { 754 console.error(`failed to get attribute value, because ${JSON.stringify(exception)}`); 755} 756``` 757### attributeValue 758 759attributeValue\<T extends keyof ElementAttributeValues>(attributeName: T, 760 callback: AsyncCallback\<ElementAttributeValues[T]>): void; 761 762Obtains the attribute value based on an attribute name. This API uses an asynchronous callback to return the result. 763 764**System capability**: SystemCapability.BarrierFree.Accessibility.Core 765 766**Parameters** 767 768| Name | Type | Mandatory | Description | 769| ------------- | ---------------------------------------- | ---- | ---------------------- | 770| attributeName | ElementAttributeKeys | Yes | Attribute name. | 771| callback | AsyncCallback<ElementAttributeValues[T]> | Yes | Callback used to return the attribute value.| 772 773**Error codes** 774 775For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 776 777| ID | Error Message | 778| ------- | ----------------------------- | 779| 9300004 | This property does not exist. | 780 781**Example** 782 783```ts 784import { ElementAttributeKeys } from '@ohos.application.AccessibilityExtensionAbility'; 785import { BusinessError } from '@ohos.base'; 786 787let attributeName: ElementAttributeKeys = 'bundleName'; 788let attributeValue: string; 789try { 790 rootElement.attributeValue(attributeName, (err: BusinessError<void>, data: string) => { 791 if (err) { 792 console.error(`failed to get attribute value, because ${JSON.stringify(err)}`); 793 return; 794 } 795 attributeValue = data; 796 console.info('get attribute value success'); 797 }); 798} catch (exception) { 799 console.error(`failed to get attribute value, because ${JSON.stringify(exception)}`); 800} 801``` 802### actionNames 803 804actionNames(): Promise\<Array\<string>>; 805 806Obtains the names of all actions supported by this element. This API uses a promise to return the result. 807 808**System capability**: SystemCapability.BarrierFree.Accessibility.Core 809 810**Return value** 811 812| Type | Description | 813| ---------------------------------- | -------------------------- | 814| Promise<Array<string>> | Promise used to return the names of all actions supported by the element.| 815 816**Example** 817 818```ts 819let actionNames: string[]; 820rootElement.actionNames().then((data: string[]) => { 821 console.log('get action names success'); 822 actionNames = data; 823}).catch((err: object) => { 824 console.error(`failed to get action names because ${JSON.stringify(err)}`); 825}) 826``` 827### actionNames 828 829actionNames(callback: AsyncCallback\<Array\<string>>): void; 830 831Obtains the names of all actions supported by this element. This API uses an asynchronous callback to return the result. 832 833**System capability**: SystemCapability.BarrierFree.Accessibility.Core 834 835**Parameters** 836 837| Name | Type | Mandatory | Description | 838| -------- | ---------------------------------------- | ---- | --------------------- | 839| callback | AsyncCallback<Array<string>> | Yes | Callback used to return the names of all actions supported by the element.| 840 841**Example** 842 843```ts 844let actionNames: string[]; 845rootElement.actionNames((err: BusinessError<void>, data: string[]) => { 846 if (err) { 847 console.error(`failed to get action names, because ${JSON.stringify(err)}`); 848 return; 849 } 850 actionNames = data; 851 console.info('get action names success'); 852}) 853``` 854### performAction 855 856performAction(actionName: string, parameters?: object): Promise\<void>; 857 858Performs an action based on the specified action name. This API uses a promise to return the result. 859 860**System capability**: SystemCapability.BarrierFree.Accessibility.Core 861 862**Parameters** 863 864| Name | Type | Mandatory | Description | 865| ----------- | ---------------------------------------- | ---- | -------------- | 866| actionName | string | Yes | Action name. For details, see [Action](./js-apis-accessibility.md#action). 867| parameters | object | No | Parameters required for performing the target action. Empty by default. Not supported currently. | 868 869**Return value** 870 871| Type | Description | 872| ------------------- | ---------------- | 873| Promise<void> | Promise that returns no value.| 874 875**Error codes** 876 877For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 878 879| ID | Error Message | 880| ------- | ----------------------------- | 881| 9300005 | This action is not supported. | 882 883**Example** 884 885```ts 886let actionName = 'action'; 887try { 888 rootElement.performAction(actionName).then(() => { 889 console.info('perform action success'); 890 }).catch((err: object) => { 891 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 892 }); 893} catch (exception) { 894 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 895} 896``` 897### performAction 898 899performAction(actionName: string, callback: AsyncCallback\<void>): void; 900 901Performs an action based on the specified action name. This API uses an asynchronous callback to return the result. 902 903**System capability**: SystemCapability.BarrierFree.Accessibility.Core 904 905**Parameters** 906 907| Name | Type | Mandatory | Description | 908| ----------- | ---------------------------------------- | ---- | -------------- | 909| actionName | string | Yes | Action name. For details, see [Action](./js-apis-accessibility.md#action). 910| callback | AsyncCallback<void> | Yes | Callback used to return the result.| 911 912**Error codes** 913 914For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 915 916| ID | Error Message | 917| ------- | ----------------------------- | 918| 9300005 | This action is not supported. | 919 920**Example** 921 922```ts 923import { BusinessError } from '@ohos.base'; 924 925let actionName = 'action'; 926try { 927 rootElement.performAction(actionName, (err:BusinessError) => { 928 if (err) { 929 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 930 return; 931 } 932 console.info('perform action success'); 933 }); 934} catch (exception) { 935 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 936} 937``` 938### performAction 939 940performAction(actionName: string, parameters: object, callback: AsyncCallback\<void>): void; 941 942Performs an action based on the specified action name. This API uses an asynchronous callback to return the result. 943 944**System capability**: SystemCapability.BarrierFree.Accessibility.Core 945 946**Parameters** 947 948| Name | Type | Mandatory | Description | 949| ---------- | ------------------------- | ---- | ---------------------------------------- | 950| actionName | string | Yes | Action name. For details, see [Action](./js-apis-accessibility.md#action).| 951| parameters | object | Yes | Parameters required for performing the target action. Empty by default. Not supported currently. | 952| callback | AsyncCallback<void> | Yes | Callback used to return the result. | 953 954**Error codes** 955 956For details about the error codes, see [Accessibility Error Codes](../errorcodes/errorcode-accessibility.md). 957 958| ID | Error Message | 959| ------- | ----------------------------- | 960| 9300005 | This action is not supported. | 961 962**Example** 963 964```ts 965import { BusinessError } from '@ohos.base'; 966 967let actionName = 'action'; 968let parameters: object = []; 969try { 970 rootElement.performAction(actionName, parameters, (err: BusinessError<void>) => { 971 if (err) { 972 console.error(`failed to perform action, because ${JSON.stringify(err)}`); 973 return; 974 } 975 console.info('perform action success'); 976 }); 977} catch (exception) { 978 console.error(`failed to perform action, because ${JSON.stringify(exception)}`); 979} 980``` 981### findElement('content') 982 983findElement(type: 'content', condition: string): Promise\<Array\<AccessibilityElement>>; 984 985Queries the element information of the **content** type. This API uses a promise to return the result. 986 987**System capability**: SystemCapability.BarrierFree.Accessibility.Core 988 989**Parameters** 990 991| Name | Type | Mandatory | Description | 992| --------- | ------ | ---- | ----------------------------- | 993| type | string | Yes | Information type. The value is fixed at **'content'**.| 994| condition | string | Yes | Search criteria. | 995 996**Return value** 997 998| Type | Description | 999| ---------------------------------------- | ----------------------------- | 1000| Promise<Array<AccessibilityElement>> | Promise used to return the result.| 1001 1002**Example** 1003 1004```ts 1005let condition = 'keyword'; 1006let elements: AccessibilityElement[]; 1007try { 1008 rootElement.findElement('content', condition).then((data: AccessibilityElement[]) => { 1009 elements = data; 1010 console.log('find element success'); 1011 }).catch((err: object) => { 1012 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1013 }); 1014} catch (exception) { 1015 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1016} 1017``` 1018### findElement('content') 1019 1020findElement(type: 'content', condition: string, callback: AsyncCallback\<Array\<AccessibilityElement>>): void; 1021 1022Queries the element information of the **content** type. This API uses an asynchronous callback to return the result. 1023 1024**System capability**: SystemCapability.BarrierFree.Accessibility.Core 1025 1026**Parameters** 1027 1028| Name | Type | Mandatory | Description | 1029| --------- | ---------------------------------------- | ---- | ---------------------------- | 1030| type | string | Yes | Information type. The value is fixed at **'content'**.| 1031| condition | string | Yes | Search criteria. | 1032| callback | AsyncCallback<Array<AccessibilityElement>> | Yes | Callback used to return the result. | 1033 1034**Example** 1035 1036```ts 1037import { BusinessError } from '@ohos.base'; 1038 1039let condition = 'keyword'; 1040let elements: AccessibilityElement[]; 1041try { 1042 rootElement.findElement('content', condition, (err: BusinessError<void>, data: AccessibilityElement[]) => { 1043 if (err) { 1044 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1045 return; 1046 } 1047 elements = data; 1048 console.info('find element success'); 1049 }); 1050} catch (exception) { 1051 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1052} 1053``` 1054### findElement('focusType') 1055 1056findElement(type: 'focusType', condition: FocusType): Promise\<AccessibilityElement>; 1057 1058Queries the element information of the **focusType** type. This API uses a promise to return the result. 1059 1060**System capability**: SystemCapability.BarrierFree.Accessibility.Core 1061 1062**Parameters** 1063 1064| Name | Type | Mandatory | Description | 1065| --------- | ----------------------- | ---- | ---------------------------------- | 1066| type | string | Yes | Information type. The value is fixed at **'focusType'**.| 1067| condition | [FocusType](#focustype) | Yes | Enumerates the focus types. | 1068 1069**Return value** 1070 1071| Type | Description | 1072| ----------------------------------- | ------------------------------ | 1073| Promise<AccessibilityElement> | Promise used to return the result.| 1074 1075**Example** 1076 1077```ts 1078import { FocusType } from '@ohos.application.AccessibilityExtensionAbility'; 1079 1080let condition: FocusType = 'normal'; 1081let element: AccessibilityElement; 1082try { 1083 rootElement.findElement('focusType', condition).then((data: AccessibilityElement) => { 1084 element = data; 1085 console.log('find element success'); 1086 }).catch((err: object) => { 1087 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1088 }); 1089} catch (exception) { 1090 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1091} 1092``` 1093### findElement('focusType') 1094 1095findElement(type: 'focusType', condition: FocusType, callback: AsyncCallback\<AccessibilityElement>): void; 1096 1097Queries the element information of the **focusType** type. This API uses an asynchronous callback to return the result. 1098 1099**System capability**: SystemCapability.BarrierFree.Accessibility.Core 1100 1101**Parameters** 1102 1103| Name | Type | Mandatory | Description | 1104| --------- | ---------------------------------------- | ---- | ---------------------------------- | 1105| type | string | Yes | Information type. The value is fixed at **'focusType'**.| 1106| condition | [FocusType](#focustype) | Yes | Enumerates the focus types. | 1107| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the result. | 1108 1109**Example** 1110 1111```ts 1112import { FocusType } from '@ohos.application.AccessibilityExtensionAbility'; 1113import { BusinessError } from '@ohos.base'; 1114 1115let condition: FocusType = 'normal'; 1116let element: AccessibilityElement; 1117try { 1118 rootElement.findElement('focusType', condition, (err: BusinessError<void>, data: AccessibilityElement) => { 1119 if (err) { 1120 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1121 return; 1122 } 1123 element = data; 1124 console.info('find element success'); 1125 }); 1126} catch (exception) { 1127 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1128} 1129``` 1130### findElement('focusDirection') 1131 1132findElement(type: 'focusDirection', condition: FocusDirection): Promise\<AccessibilityElement>; 1133 1134Queries the element information of the **focusDirection** type. This API uses a promise to return the result. 1135 1136**System capability**: SystemCapability.BarrierFree.Accessibility.Core 1137 1138**Parameters** 1139 1140| Name | Type | Mandatory | Description | 1141| --------- | --------------------------------- | ---- | ---------------------------------------- | 1142| type | string | Yes | Information type. The value is fixed at **'focusDirection'**.| 1143| condition | [FocusDirection](#focusdirection) | Yes | Enumerates the focus directions. | 1144 1145**Return value** 1146 1147| Type | Description | 1148| ----------------------------------- | -------------------------------- | 1149| Promise<AccessibilityElement> | Promise used to return the result.| 1150 1151**Example** 1152 1153```ts 1154import { FocusDirection } from '@ohos.application.AccessibilityExtensionAbility'; 1155 1156let condition: FocusDirection = 'up'; 1157let element: AccessibilityElement; 1158try { 1159 rootElement.findElement('focusDirection', condition).then((data: AccessibilityElement) => { 1160 element = data; 1161 console.log('find element success'); 1162 }).catch((err: object) => { 1163 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1164 }); 1165} catch (exception) { 1166 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1167} 1168``` 1169### findElement('focusDirection') 1170 1171findElement(type: 'focusDirection', condition: FocusDirection, callback: AsyncCallback\<AccessibilityElement>): void; 1172 1173Queries the element information of the **focusDirection** type. This API uses an asynchronous callback to return the result. 1174 1175**System capability**: SystemCapability.BarrierFree.Accessibility.Core 1176 1177**Parameters** 1178 1179| Name | Type | Mandatory | Description | 1180| --------- | ---------------------------------------- | ---- | ---------------------------------------- | 1181| type | string | Yes | Information type. The value is fixed at **'focusDirection'**.| 1182| condition | [FocusDirection](#focusdirection) | Yes | Direction of the next focus element. | 1183| callback | AsyncCallback<AccessibilityElement> | Yes | Callback used to return the result. | 1184 1185**Example** 1186 1187```ts 1188import { FocusDirection } from '@ohos.application.AccessibilityExtensionAbility'; 1189import { BusinessError } from '@ohos.base'; 1190 1191let condition: FocusDirection = 'up'; 1192let elements: AccessibilityElement; 1193try { 1194 rootElement.findElement('focusDirection', condition, (err: BusinessError<void>, data: AccessibilityElement) => { 1195 if (err) { 1196 console.error(`failed to find element, because ${JSON.stringify(err)}`); 1197 return; 1198 } 1199 elements = data; 1200 console.info('find element success'); 1201 }); 1202} catch (exception) { 1203 console.error(`failed to find element, because ${JSON.stringify(exception)}`); 1204} 1205``` 1206