[JS] 刪除臉書社團腳本(Delete Facebook Group)


因為有需求就去寫了個簡單的腳本,然後我 js 真的很爛只會這種基礎應用,不要叫我去寫表單拉 (再度偏題

因為懶得去碰可怕的 Facebook API,直接以腳本的形式完成...

完成腳本需要取得的幾個東西:
齒輪按鈕(uiButton)
<a class="uiPopoverButton _p uiButton uiButtonSuppressed uiButtonNoText" href="#" role="button" aria-haspopup="true" aria-expanded="false" rel="toggle" id="u_0_8x" aria-owns="u_0_3m"><i class="mrs img sp_ev8j7x sx_21b848"></i><span class="uiButtonText"></span></a>
我取得的寫法:
document.getElementsByClassName("uiPopoverButton _p uiButton uiButtonSuppressed uiButtonNoText")[0].click();

刪除成員(Remove from Group)
<a class="_54nc" tabindex="0" href="/ajax/groups/members/remove.php?group_id=639712429389169&amp;uid=100000432257729" rel="dialog-post" role="menuitem" title=""><span class="_54nh">Remove from Group</span></a>
我取得的寫法:
document.getElementsByClassName("uiContextualLayer uiContextualLayerBelowLeft")[0].getElementsByTagName("a")[1].click();

確認刪除(Confirm)
<button value="1" class="_42ft _42fu layerConfirm uiOverlayButton selected _42g- _42gy" type="submit">Confirm</button>
我取得的寫法:
document.getElementsByClassName("pam uiOverlayFooter uiBoxGray topborder")[0].getElementsByTagName("button")[0].click();


這次遇到幾個小問題:
  1. 執行動作後等待 UI 反應時間
  2. 刪除後結構(節點)存留
  3. 避開執行刪除者

第一個問題很好解決,直接用 SetTimeout 搞定,不過每台電腦的時間會有微妙的差異所以要自行微調。

第二個的話是寫到後來才發現的問題,FB 在移除成員之後在 Html 中的部分沒有順便刪掉,導致再判斷的時候會按到前一個人的「移除連結」導致錯誤(如圖)


一開始我想到的解決方法很簡單,迴圈的時候自動 i++ 來判斷第二個取得「刪除成員」的陣列序號,這方法還可以不過後來有想到另一種寫法...

手動刪除節點
var rg = document.getElementsByClassName("uiContextualLayer uiContextualLayerBelowLeft")[0];
rg.parentNode.removeChild(rg);

這樣就不會取到上一個成員的「移除連結」了。

第三個問題其實還好,我本來是寫了一個「刪除者判斷」,後來發現其實在預設的社團成員名單(All Members)本身的帳號一定是在第一個,所以只要把我們第一個取得的「齒輪按鈕」陣列取到第二順位就不會刪除到自己啦!

還是附上當初寫的避開刪除者,一樣是使用移除節點的做法...
var admin = "執行刪除者的名字"; //遇到刪除者則跳過
var uiButton = document.getElementsByClassName("uiPopoverButton _p uiButton uiButtonSuppressed uiButtonNoText")[0];
if(uiButton.parentNode.parentNode.parentNode.firstChild.innerText == admin){uiButton.parentNode.removeChild(uiButton);}

大致上這樣就完成了,親手把自己第一個創立的社團刪掉了真是感傷啊嗚嗚 (哭屁
這邊再附上完整的腳本,有問題就把幾個參數調整看看拉~
function RemoveGroup() {
 setTimeout("document.getElementsByClassName('uiPopoverButton _p uiButton uiButtonSuppressed uiButtonNoText')[1].click();", 300);
 setTimeout("document.getElementsByClassName('uiContextualLayer uiContextualLayerBelowLeft')[0].getElementsByTagName('a')[1].click();", 2300);
 setTimeout("document.getElementsByClassName('pam uiOverlayFooter uiBoxGray topborder')[0].getElementsByTagName('button')[0].click();", 3900);
 var rg = document.getElementsByClassName('uiContextualLayer uiContextualLayerBelowLeft')[0];
 rg.parentNode.removeChild(rg);
 setTimeout("RemoveGroup();", 4300);
};
RemoveGroup();

備註:如果有按鈕沒正確取得的話就 index +-1 試試看吧~

留言

本月最夯

楓之谷洋蔥4.1.2 - 最後更新日期04/03

偷用電腦,怎知?事件檢視器全記錄!(開機時間、啟動項時間...)