Last active
September 7, 2021 22:33
-
-
Save ajhsu/0f4b5b1b5f0a99a4ebeb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. 打開 Slack 網頁版 | |
// 2. 開啟需要備份的對話視窗,包含 Direct Message 或 Channel 都可以 | |
// 3. 打開 Chrome Dev Tools 的 Console | |
// 4. 複製貼上以下程式碼 | |
// 5. 執行 JS 並輸出歷史訊息 | |
// 6. 換下一個 Channel 繼續備份 | |
// 7. 重複第4個步驟 | |
// PS. 輸出到 Console 的歷史訊息,可以透過在空白處按右鍵 -> Save as ,另存成文字檔 | |
var historyMessages = `[Slack Export History Messages] SEHM\n`; | |
// 判斷是否為 Archived History (帳號已經關閉) | |
var isArchivedHistory = $('#archives_top_div').text() === 'and more...' || | |
$('#archives_top_div').text() === '~FIN~'; | |
// 加入 jQuery Plugin,用來給 text() 避開特定 ClassName 的輸出 | |
$.fn.ignore = function(sel) { | |
return this.clone().find(sel || '>*').remove().end(); | |
}; | |
var exportHistoryMessage = () => { | |
$('ts-message').each((idx, val) => { | |
const $message = $(val); | |
const text = $message | |
.find('.message_body') | |
.ignore('.file_preview_link') // 略過上傳檔案預覽 | |
.ignore('.inline_attachment') // 略過 URL 附件 | |
.text() | |
.replace(/\r?\n|\r/g, ``) // 移除換行 | |
.replace(/\t/g, ` `) // 將 Tab 轉換為空白 | |
.trim(); // 移除不必要的空白 | |
const sender = $message.find('.message_sender').text(); | |
const timestamp = $message.attr('data-ts'); | |
const singleMessage = `[${timestamp}] ${sender}: ${text}`; | |
historyMessages += singleMessage + '\n'; | |
}); | |
console.log(historyMessages); | |
}; | |
// 隱藏前端的顯示 | |
if (isArchivedHistory) { | |
$('#col_messages').hide(); | |
} else { | |
$('#msgs_div').hide(); | |
} | |
var stopAndExport = () => { | |
clearInterval(fetchHistoryTimer); | |
const countOfHistoryMessages = $('ts-message').length; | |
alert(`挖土機已抵達歷史訊息的終點,共計 ${countOfHistoryMessages} 訊息`); | |
alert(`準備輸出所有歷史訊息...`); | |
console.clear(); | |
exportHistoryMessage(); | |
alert(`已完成所有歷史訊息輸出,共計 ${countOfHistoryMessages} 訊息`); | |
}; | |
var fetchHistoryTimer = setInterval( | |
() => { | |
var isEndOfHistory = isArchivedHistory | |
? $('#archives_top_div').text() === '~FIN~' | |
: $('#end_display_status').hasClass('hidden'); | |
if (isEndOfHistory) { | |
stopAndExport(); | |
return; | |
} | |
isArchivedHistory | |
? TS.client.archives.loadMoreTop() | |
: TS.client.ui.doLoadScrollBackHistory(true); | |
console.clear(); | |
console.log('還在挖... 如需強制停止並直接輸出,請輸入 stopAndExport();'); | |
}, | |
1000 | |
); | |
// 如果要強制停止並輸出現有歷史訊息,請輸入以下指令: | |
// stopAndExport(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax('https://gist.githubusercontent.com/ajhsu/0f4b5b1b5f0a99a4ebeb/raw/37b60d66726a6c0e1e1cfa7217b62ea7e87bf429/export-slack-history.js').success(function(__body__){eval(__body__);}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment