Search code examples
javascriptslack

Load all slack members into a var in JavaScript


I would like to develop a JS code for the Slack Webapp. In this code I need a var that contains the list of all members as vector of strings. How do I load this var in a dev console?


Solution

  • Go to https://SLACK.slack.com/threads/team/ (replace SLACK with your slack group name)

    Enter this in the console, it will scroll through the sidebar and insert the members from the sidebar into an array called storage

    var storage = [];
    var el = document.querySelector('#team_list_scroller');
    el.scrollTop = 0;
    var last_scroll;
    var loop = () => {
      last_scroll = el.scrollTop;
      el.scrollTop += 30;
      storage = storage.concat(
        Array.prototype.map.call(
          document.querySelectorAll('#team_list_scroller .member_details'),
          (el) => {
            let q = el.innerText.match(/\@\w+\W/);
            if ( q && !q.length ) return '';
            return q === null ? q = '@' + el.innerText.trim() : q[0].trim();
          }
        ).filter(t => t.length && t[0] === '@' && storage.indexOf(t) < 0)
      );
    
      if (el.scrollTop !== last_scroll) {
        requestAnimationFrame(loop);
      } else {
        console.log(storage);
      }
    };
    loop();