Source: selector/methods/IframeSelector.js

(function() {
    /** @augments Help4.selector.methods.BaseSelector */
    Help4.selector.methods.IframeSelector = class extends Help4.selector.methods.BaseSelector {
        /** @type {Object} */
        static CONFIG = {attributes: ['id', 'src']};

        /**
         * @param {string} selector
         * @return {?HTMLIFrameElement}
         */
        static getElement(selector) {
            const {CLASS_PREFIX, Element, selector: {methods}} = Help4;
            const iframes = /** @type {NodeList<HTMLIFrameElement>} */ methods.$(selector);

            let first = /** @type {?HTMLIFrameElement} */ null;
            for (const iframe of iframes) {
                if (iframe.className.indexOf(CLASS_PREFIX) >= 0) continue;  // filter our own iframe(s)
                if (Element.isVisible(iframe)) return iframe;  // return the 1st visible one
                first ||= iframe;  // save the first iframe, that is not our own
            }
            return first;  // none is visible; return the 1st
        }

        /**
         * @param {HTMLElement} element
         * @param {string} [domIdPrefix = null]
         * @return {?Help4.selector.methods.Selector}
         */
        static getSelector(element, domIdPrefix = null) {
            if (element.nodeName !== 'IFRAME') return null;

            const {
                selector: {methods, SELECTOR_QUALITY: {good: quality}},
                CLASS_PREFIX
            } = Help4;
            const {CONFIG} = this;

            const attributes = CONFIG.attributes
            .map(attribute => element.hasAttribute(attribute) ? `[${attribute}]` : '')
            .join('');

            const value = `IFRAME${attributes}`;
            const iframes = /** @type {NodeList<HTMLIFrameElement>} */ methods.$(value);
            let l = iframes.length;

            for (/** @type {HTMLIFrameElement} */ const {className, id} of iframes) {
                if (className.indexOf(CLASS_PREFIX) >= 0 ||  // filter our own iframe(s)
                    domIdPrefix && typeof id === 'string' && id.indexOf(domIdPrefix) !== 0)  // and take care of DOM ID prefix
                {
                    l--;
                }
            }

            return l === 1
                ? {value, quality}
                : null;
        }
    }
})();