// ==UserScript== // @name 罗盘直播大屏弹幕监听 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.3.0 // @description 监听直播间弹幕 // @author 汪喜 // @match https://compass.jinritemai.com/screen/live* // @grant GM_xmlhttpRequest // @require https://scriptcat.org/lib/637/1.4.3/ajaxHooker.js#sha256=y1sWy1M/U5JP1tlAY5e80monDp27fF+GMRLsOiIrSUY= // ==/UserScript== (function () { 'use strict'; //一些更改 // 添加一个按钮来控制监听状态 function addToggleButton() { // 使用setTimeout来延迟按钮的添加 setTimeout(() => { const targetContainerSelector = '.header--XNxuk'; const targetContainer = document.querySelector(targetContainerSelector); if (!targetContainer) { console.error('The target container for the toggle button was not found.'); return; } const button = document.createElement('button'); button.textContent = '开始监听'; button.style.marginLeft = '10px'; let isListening = false; button.addEventListener('click', () => { if (isListening) { ah.unProxy(); button.textContent = '开始监听'; isListening = false; alert('监听结束'); } else { startListening(); button.textContent = '监听中 点击停止'; isListening = true; alert('监听开始'); } }); targetContainer.appendChild(button); }, 3000); // 延迟3000毫秒(3秒)后执行 } window.addEventListener('load', addToggleButton); const appId = "cli_a6f25876ea28100d"; const appSecret = "raLC56ZLIara07nKigpysfoDxHTAeyJf"; const appToken = 'HyV1bstZra3P1xstDiecfCSjnLe'; //多维表格appToken const tableId = 'tblEqZ2x8eujgl0c'; //多维表格tableId class FeishuAPI { constructor() { this.tenantAccessToken = null; } async getTenantAccessToken() { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: "POST", url: "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/", headers: { "Content-Type": "application/json", "accept": "application/json, text/plain, */*" }, data: JSON.stringify({ "app_id": appId, "app_secret": appSecret }), onload: (response) => { if (response.status === 200 && response.responseText) { let responseData = JSON.parse(response.responseText); this.tenantAccessToken = responseData.tenant_access_token; console.log('Tenant Access Token:', this.tenantAccessToken); resolve(this.tenantAccessToken); } else { console.error('Failed to get tenant access token:', response.statusText); reject(new Error('Failed to get tenant access token')); } } }); }); } async writeIntoBase(dataItem) { if (!this.tenantAccessToken) { await this.getTenantAccessToken(); } return new Promise((resolve, reject) => { const headers = { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.tenantAccessToken}`, }; const requestBody = { fields: dataItem }; GM_xmlhttpRequest({ method: 'POST', url: `https://open.feishu.cn/open-apis/bitable/v1/apps/${appToken}/tables/${tableId}/records`, headers: headers, data: JSON.stringify(requestBody), onload: function (response) { if (response.status === 200) { console.log('Data written to Feishu successfully:', response.responseText); resolve(response.responseText); } else { console.error('Failed to write data:', response.statusText); reject(new Error(`HTTP error! Status: ${response.status}`)); } }, onerror: function (error) { console.error('Request failed:', error); reject(new Error('Request failed')); } }); }); } } function startListening() { ajaxHooker.hook(request => { if (request.url.includes('https://compass.jinritemai.com/business_api/shop/live_bigscreen/comment')) { request.response = async res => { const jsonResponse = await res.json(); const comments = jsonResponse.data.comments; comments.forEach(async (comment) => { const dataItem = { nickname: comment.nick_name, content: comment.content, createtime: Math.round(new Date().getTime()) }; console.log(dataItem); const feishuAPI = new FeishuAPI(); await feishuAPI.writeIntoBase(dataItem); }); }; } }); } })();