Source: selector/methods/AppFrameSelector.js

(function() {
    /** @augments Help4.selector.methods.BaseSelector */
    Help4.selector.methods.AppFrameSelector = class extends Help4.selector.methods.BaseSelector {
        /**
         * @param {string} selector
         * @return {?HTMLElement}
         */
         static getElement(selector) {
            selector = Help4.JSON.parse(selector);
            const {type, recordingIdx} = selector;

            const crossOriginEngine = Help4.getController().getEngine('crossOrigin');
            const {AGENT_TYPE: {recording} = {}} = crossOriginEngine || {};
            const iframes = /** @type {NodeList<HTMLIFrameElement>} */ Help4.selector.methods.$('iframe');

            let idx = 0;
            for (/** @type {HTMLIFrameElement} */ const iframe of iframes) {
                if (Help4.isHelp4Iframe(iframe)) continue;

                switch (type) {
                    case 'WDA':
                    case 'HTMLGUI':
                        if (!recordingIdx || recordingIdx === idx++) {
                            delete selector.recordingIdx;  // only for rerecording during recording phase!
                            return iframe;
                        }
                        break;

                    case 'AGENT':
                        if (crossOriginEngine?.hasAgent({window: iframe.contentWindow, type: recording})) {
                            if (!recordingIdx || recordingIdx === idx++) {
                                delete selector.recordingIdx;  // only for rerecording during recording phase!
                                return iframe;
                            }
                        }
                        break;
                }
            }

            return null;
        }

        /**
         * @param {HTMLElement} element
         * @return {?Help4.selector.methods.Selector}
         */
         static getSelector(element) {
            const {methods, SELECTOR_QUALITY: {best: quality}} = Help4.selector;
            const value = {};

            if (element.nodeName === 'IFRAME' && !Help4.isHelp4Iframe(element)) {
                const controller = Help4.getController();
                const crossOriginEngine = controller.getEngine('crossOrigin');
                const {AGENT_TYPE: {recording: type} = {}} = crossOriginEngine || {};

                const iframes = /** @type {NodeList<HTMLIFrameElement>} */ methods.$('iframe');
                let idx = 0;

                const mode = controller.getConfiguration().fioriApplication;
                if (mode) {
                    // WDA, HTML GUI
                    // - currently only same-origin
                    // - currently only one per screen
                    value.type = mode;  // WDA, HTMLGUI

                    for (/** @type {HTMLIFrameElement} */ const iframe of iframes) {
                        if (Help4.isHelp4Iframe(iframe)) continue;
                        if (element === iframe) break;
                        idx++;
                    }
                } else if (crossOriginEngine?.hasAgent({window: element.contentWindow, type})) {
                    // cross-origin iframe
                    // - could be more than one per screen
                    value.type = 'AGENT';

                    for (/** @type {HTMLIFrameElement} */ const iframe of iframes) {
                        if (Help4.isHelp4Iframe(iframe)) continue;
                        if (element === iframe) break;
                        if (crossOriginEngine.hasAgent({window: iframe.contentWindow, type})) idx++;
                    }
                }

                // required for rerecording during recording process
                // not useful for rerecording during playback!
                if (value.type && idx) value.recordingIdx = idx;
            }

            return value.type
                ? {value: Help4.JSON.stringify(value), quality}
                : null;
        }
    }
})();