|
// ==UserScript== |
|
// @name GitHub PR DingTalk Share Configurable |
|
// @namespace https://nowtime.cc |
|
// @version 2025-10-16 |
|
// @description 一键发送 PR 链接到钉钉用户 |
|
// @author Shine |
|
// @match https://github.com/*/pull/* |
|
// @grant GM_setValue |
|
// @grant GM_getValue |
|
// ==/UserScript== |
|
|
|
(function() { |
|
'use strict'; |
|
|
|
async function getDingTalkId() { |
|
let id = await GM_getValue('dingtalk_id', ''); |
|
if (!id) { |
|
id = prompt('请输入你的 DingTalk ID:'); |
|
if (id) { |
|
await GM_setValue('dingtalk_id', id); |
|
} |
|
} |
|
return id; |
|
} |
|
|
|
function createConfigButton(dingTab) { |
|
// 避免重复插入 |
|
if (document.querySelector('#dingTalk-config-btn')) return; |
|
|
|
const btn = document.createElement('button'); |
|
btn.textContent = '⚙️'; |
|
btn.id = 'dingTalk-config-btn'; |
|
btn.style.marginRight = '4px'; |
|
btn.style.cursor = 'pointer'; |
|
btn.title = '编辑 DingTalk ID'; |
|
|
|
btn.addEventListener('click', async () => { |
|
const oldId = await GM_getValue('dingtalk_id', ''); |
|
const newId = prompt('请输入新的 DingTalk ID:', oldId); |
|
if (newId) { |
|
await GM_setValue('dingtalk_id', newId); |
|
alert('已更新 DingTalk ID: ' + newId); |
|
} |
|
}); |
|
|
|
// 插入到 dingTab 左侧 |
|
dingTab.parentElement.insertBefore(btn, dingTab); |
|
} |
|
|
|
async function insertDingTab() { |
|
const filesChangedTab = document.querySelector('nav[data-pjax="#repo-content-pjax-container"] a:nth-child(4)'); |
|
if (!filesChangedTab) return; |
|
|
|
// 避免重复插入 |
|
if (document.querySelector('#dingTalk-share-tab')) return; |
|
|
|
const dingTab = filesChangedTab.cloneNode(true); |
|
dingTab.textContent = 'Send to DingTalk'; |
|
dingTab.removeAttribute('data-tab-item'); |
|
dingTab.removeAttribute('href'); |
|
dingTab.style.cursor = 'pointer'; |
|
dingTab.id = 'dingTalk-share-tab'; |
|
|
|
filesChangedTab.parentElement.insertBefore(dingTab, filesChangedTab.nextSibling); |
|
|
|
createConfigButton(dingTab); |
|
|
|
dingTab.addEventListener('click', async e => { |
|
e.preventDefault(); |
|
let id = await getDingTalkId(); |
|
if (!id) return; |
|
|
|
const currentUrl = filesChangedTab.href; |
|
await navigator.clipboard.writeText(currentUrl); |
|
console.log('已复制链接:', currentUrl); |
|
|
|
const dingUrl = `dingtalk://dingtalkclient/action/sendmsg?dingtalk_id=${id}`; |
|
window.open(dingUrl, '_self'); |
|
}); |
|
} |
|
|
|
// 监听 DOM 变化 |
|
const observer = new MutationObserver(() => { |
|
insertDingTab(); |
|
}); |
|
observer.observe(document.body, { childList: true, subtree: true }); |
|
|
|
// 首次执行 |
|
insertDingTab(); |
|
})(); |