Функция стрелки Refactor для совместимости с IE - PullRequest
0 голосов
/ 27 февраля 2019

У меня есть скрипт для проверки появления капчи, но функции стрелки не работают в IE.

function initListener() {

              // set a global to tell that we are listening
              window.recaptchaCloseListener = true

              // find the open reCaptcha window
              HTMLCollection.prototype.find = Array.prototype.find
              var recaptchaWindow = document
                  .getElementsByTagName('iframe')
                  .find(x=>x.src.includes('google.com/recaptcha/api2/bframe'))
                    .parentNode.parentNode



              // and now we are listening on CSS changes on it
              // when the opacity has been changed to 0 we know that
              // the window has been closed
              new MutationObserver(x => recaptchaWindow.style.opacity == 0 && onClose())
                  .observe(recaptchaWindow, { attributes: true, attributeFilter: ['style'] })

            }

Две строки проблематичны:

.find(x=>x.src.includes('google.com/recaptcha/api2/bframe'))
                    .parentNode.parentNode

и:

new MutationObserver(x => recaptchaWindow.style.opacity == 0 && onClose())

Как мне провести рефакторинг этих двух строк?

1 Ответ

0 голосов
/ 27 февраля 2019

Для этого решения требуются полифилы для .include () и Array.from (), найденные ниже:

Array.from в Internet Explorer

т.е. не поддерживает метод 'includes'

и обновленный код:

функция initListener () {

              // set a global to tell that we are listening
              window.recaptchaCloseListener = true

              // find the open reCaptcha window

                    var frames = Array.from(document.getElementsByTagName('iframe'));
                    var recaptchaWindow;

                    frames.forEach(function(x){

                        if (x.src.includes('google.com/recaptcha/api2/bframe') ){
                            recaptchaWindow = x.parentNode.parentNode;
                        };

                    });

              // and now we are listening on CSS changes on it
              // when the opacity has been changed to 0 we know that
                // the window has been closed

                new MutationObserver(function(){
                    recaptchaWindow.style.opacity == 0 && onClose();
                })
                  .observe(recaptchaWindow, { attributes: true, attributeFilter: ['style'] })

            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...