halfdan / duolingo-skill-strength

Skill Strength Script for Duolingo
MIT License
2 stars 4 forks source link

The skills do not update after switching languages #3

Open franzalex opened 7 years ago

franzalex commented 7 years ago

With the current script on the new website, I have noticed that the skills do not get updated when a new language is selected. One has to refresh the entire page after switching languages in order to get the skill strengths to display the values for the current language.

I was under the impression that some post-AJAX code had been removed but a comparison with fce2759388 does not indicate that any such code has been changed. Could you please look into this?

franzalex commented 7 years ago

I performed a test to find out if the relevant methods are invoked after the language is switched. Unfortunately, it appears that the ajaxComplete event is not raised after a language switch occurs.

Below is the userscript I used for my tests. It is based on a stripped down version of your code as of c8573038dd.

// ==UserScript==
// @name         Duolingo Language Switch Test
// @namespace    default
// @description  tests if ajaxComplete is called after language is switched.
// @author       franzalex
// @match        *://www.duolingo.com/*
// @require      http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.2.1/lodash.min.js
// ==/UserScript==
console.log('Userscript loaded: Duolingo Language Switch Test');
function inject(f) { //Inject the script into the document
  var script;
  script = document.createElement('script');
  script.type = 'text/javascript';
  script.setAttribute('name', 'lang-switch-test');
  script.textContent = '(' + f.toString() + ')(jQuery)';
  document.head.appendChild(script);
}
inject(f);
function f($) {
  var logKeyword = 'DL-lang-switch-test: ';
  var isLoading = false; // prevent race conditions
  var oldLang = '';
  function doDetection() {
    if (!isLoading) {
      isLoading = true;
      $.ajax({
        url: '/vocabulary/overview',
        success: function (data) {
          newLang = data.learning_language;
          if (newLang != oldLang)
          {
            console.log(logKeyword + 'New Lang: ' + newLang.toUpperCase());
            oldLang = newLang;
            isLoading = false;
          }
        }
      });
    }
  }
  $(document).ready(function () {
    console.log(logKeyword + 'Document ready');
    doDetection();
  });
  $(document).ajaxComplete(function () {
    console.log(logKeyword + 'AJAX complete');
    doDetection();
  });
}

With this userscript installed, an examination of the console log outputs something similar to the snippet below and it stays that way regardless of how many times the language is switched.

Userscript loaded: Duolingo Language Switch Test   Duolingo_Language_Switch_Test.user.js:10:1
DL-lang-switch-test: Document ready                                     www.duolingo.com:23:5
DL-lang-switch-test: New Lang: FR                                      www.duolingo.com:14:13
DL-lang-switch-test: AJAX complete                                      www.duolingo.com:27:5
DL-lang-switch-test: AJAX complete                                      www.duolingo.com:27:5
AbsolutelyFreeWeb commented 5 years ago

please check if fixed with the pending pull request version 0.2.1