Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishimotonico/1af106be7ab9ea421d1b5dbbc04d9457 to your computer and use it in GitHub Desktop.
Save kishimotonico/1af106be7ab9ea421d1b5dbbc04d9457 to your computer and use it in GitHub Desktop.
Chatworkに、1クリックでミュート設定できるボタンを追加するJavaScriptです
/**
* Chatworkにミュートボタン追加.js
*
* ミュート設定を1クリックで済ませるためのボタンをChatworkに追加するJavaScriptです。
* 次の拡張機能などで使うことを想定しています。
*
* ScriptAutoRunner(https://chrome.google.com/webstore/detail/scriptautorunner/gpgjofmpmjjopcogjgdldidobhmjmdbm)
*
* 下記コードを理解できる方が、自己責任でご利用ください。
* また、環境によっては動かないと思うので調整してください。
*/
const sleep = ms => new Promise(res => setTimeout(res, ms));
function getElementByInnerText(query, innerText) {
return [...document.querySelectorAll(query)].find((elem) => elem.innerText === innerText);
}
const insertHtmlText = `
<div id="mycustom_mute">
<div class="_showDescription">
<button type="button">
<svg viewBox="0 0 10 10" width="16" height="16" class="sc-hKMtZM hfqvQD" aria-hidden="true"><use fill-rule="evenodd" xlink:href="#icon_mute"></use></svg>
</button>
</div>
</div>
<style>
#mycustom_mute button {
box-sizing: border-box;
width: 32px;
height: 32px;
padding: 8px;
border: none;
border-radius: 4px;
cursor: pointer;
outline: none;
background-color: transparent;
fill: rgb(127, 42, 42);
}
</style>
`;
function addMuteButtonHandler() {
if (document.getElementById("mycustom_mute") !== null) {
return;
}
const buttonsContainerElem = document.querySelector("#_roomHeader .chatRoomHeader__actionButtonContainer");
buttonsContainerElem?.insertAdjacentHTML("beforeend", insertHtmlText);
document.getElementById("mycustom_mute").addEventListener("click", async () => {
// 設定ダイアログを開く
document.querySelector("#chatRoomSetting button").click();
await sleep(40);
getElementByInnerText("li[role='menuitem']", "グループチャットの設定").click();
await sleep(80);
// ミュートタブを選択
getElementByInnerText(".dialogContainer button", "ミュート").click();
await sleep(120);
// チェックボックスをトグル
getElementByInnerText("form#_roomMuteSettingForm span", "グループチャットをミュート").click();
await sleep(40);
// 保存ボタンを押下
getElementByInnerText(".dialogContainer button", "保存").click();
});
}
// URLのハッシュ変更時にHTML要素を追加
document.addEventListener('DOMContentLoaded', addMuteButtonHandler);
window.addEventListener('hashchange', addMuteButtonHandler, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment