首页 > 解决方案 > 邀请所有团队成员加入新 Slack 频道的脚本

问题描述

我正在尝试将松弛的每个成员添加到我正在创建的新频道中。(因为有 7k+ 成员,这不是我每次出现时都想手动做的事情。)有很多旧的例子说明如何做到这一点,但由于松弛不再包括 jQuery,它们都已经过时了他们的数据库以及一些类和 id 更改。我非常接近让代码正常工作,但缺少一个关键步骤,因此寻求帮助解决。

我首先解决了这篇非常古老的帖子中提供的最后一个解决方案:https ://gist.github.com/YitzhakAndrade/1c7c2f7ac98520c9c84d536880f96770

此处的原始(但非常旧的代码)代码(注意:说明是在浏览器中浏览到适当的 slack 通道并将脚本粘贴到您的 Chrome/Firefox 开发控制台并按 Enter。):

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}
var letters = 'abcdefghijklmnopqrstuvwxyz';
async function inviteAllUsers() {      
   for(var i = 0; i < letters.length; i++){
      await sleep(300);
    $('#channel_actions_toggle').click();
      await sleep(300);
    $('#channel_invite_item').click();
      await sleep(300);
     for(var k = 0; k < letters.length; k++){
        var word = letters[i] + letters[k];
        await sleep(300);
        $("#channel_invite_filter").val(word).trigger("input");
        $(".channel_invite_member:not(hidden)").each(function(i, obj) {
            foundAny=true;
            this.click();
        });
      }
      await sleep(300);
      $('.invite_go').click()
    }
}

inviteAllUsers(); 
setInterval(inviteAllUsers,600*1000);

如上所述,jQuery 不再包含在松弛数据库中,因此代码不再有效。

这是我当前的重写版本,内联详细说明了问题:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

var simulateClick = function(elem) {
  // Create our event (with options)
  var evt = new MouseEvent("click", {
    bubbles: true,
    cancelable: true,
    view: window
  });
  // If cancelled, don't dispatch our event
  var canceled = !elem.dispatchEvent(evt);
};

// var letters = "abcdefghijklmnopqrstuvwxyz";
var letters = "ab";
async function inviteAllUsers() {
  for (var i = 0; i < letters.length; i++) {
    await sleep(300);
    // Opens up the settings menu
    simulateClick(document.querySelector(".c-icon--cog-o"));
    await sleep(300);
    // Selects the "Add People to Channel" selection
    simulateClick(
      document.querySelectorAll(".c-menu__items .c-menu_item__li button")[1]
    );
    await sleep(300);
    for (var k = 0; k < letters.length; k++) {
      var word = letters[i] + letters[k];
      console.log(word);
      await sleep(300);
      // Inputs current word into the search field
      simulateClick(document.querySelector(".c-multi-select input"));
      document.querySelector(".c-multi-select input").value = word;
      // document.querySelector(".c-multi-select input").value = 'a';
      document
        .querySelector("#channel_invite_modal_select")
        .setAttribute("value", word);
      // document.querySelector("#channel_invite_modal_select").setAttribute('value', 'a');

      // THIS IS WHERE MY ISSUE LIES
      // By just adjusting the value in this way, I'm unable to actually have slack retrieve the matching filtered slack names.
      // When interacting with slack normally, the filtered list is shown upon typing in a letter.
      // With the script, although the letter will populate and the value will change to what's passed in,
      // it's as though something just isn't being triggered.

      // Once I get it so that the options will populate, I'll want to add the list provided.

      // I'm thinking something like below should work:
      document
        .querySelectorAll(".c-select_options_list__option")
        .forEach(function(option) {
          simulateClick(option)
        });
    }

    // Then, once all the names are selected, click the "Add" button.
    simulateClick(
      document.querySelectorAll(
        ".c-button[data-qa='invite_to_workspace_submit_button']"
      )
    );

    await sleep(300);
  }
}

inviteAllUsers();
setInterval(inviteAllUsers, 600 * 1000);

正如您在代码中看到的那样,虽然我能够进入适当的模式并设法更改搜索字段中的输入值,但我目前实际上无法让 slack 检索匹配的已过滤 slack 名称。我想我只是错过了某些东西的触发,但我不知道是什么。

希望在确定我缺少哪个步骤以访问选项列表并使其正常工作方面获得任何帮助。

标签: javascriptconsoleslack

解决方案


继@SeanKilleen 之后,我实际上最近不得不这样做,并决定尝试他的方法,将 1530 个用户从一个频道复制到另一个频道。

我已经记录了我在以下要点中使用的方法和脚本:

https://gist.github.com/norganna/12686901c0d802ed112d911ee24bc68c


推荐阅读