`;
document.body.appendChild(overlay);
$(".copy-button").onclick=()=>navigator.clipboard.writeText(url).then(()=>alert("URL copied!"));
$(".close-button").onclick=()=>overlay.remove();
$(".delete-button").onclick=()=>deleteTelegraph(url).then(()=>alert(`${url} is deleted!\nClick "Share" to share it again.`));
}
// Convert DeepSeek chat messages to Telegraph content format.
function convertToTelegraphContent(messages) {
let tgphData = [];
messages.forEach(message => {
const {role,content} = message; // role: system | assistant | user
let text = `\`${role}\`: ${content}`;
if(role.toLowerCase()==="assistant"&&message.thinking_enabled&&message.thinking_content)text=`\`${role}\`: \n\n${message.thinking_content.split("\n\n").map(e=>"> "+e).join("\n\n")}\n\n${content}`;
tgphData = tgphData.concat(md2TgphNode(text));
}); //console.log(tgphData);
return JSON.stringify(tgphData);
}
var SUPPORTED_TGPH_TAGS = ["a","aside","b","blockquote","br","code","em","figcaption","figure","h3","h4","hr","i","iframe","img","li","ol","p","pre","s","strong","u","ul","video"];
function md2TgphNode(c) {
c=c.trim();
var d=new DOMParser().parseFromString(marked.parse(c),"text/html");
if(!d)throw new Error("Failed to parse HTML to DOM");
var n=domToNode(d.body);
if(!n)throw new Error(`Empty node content: ${d.body.textContent}`);
if(typeof n==="string")return n;
if(!n.children)throw new Error(`Empty content: ${c}`);
return n.children;
}
function domToNode(el) {
if(el.nodeType==Node.TEXT_NODE){var t=el.textContent;return el.parentElement?.tagName==="P"&&t?t.replace("\n"," "):t||null;}
if(!(el instanceof Element))return null;
var tg=el.tagName.toLowerCase();
if(!SUPPORTED_TGPH_TAGS.includes(tg)&&tg!=="body") console.log("domToTelegraphNode: unsupported tag: ",el.tagName,el.innerHTML);
var n={tag:tg};
if(tg==="code"&&el.parentElement?.tagName==="PRE")n.tag="pre";
var h=el.getAttribute("href");if(h!=null)n.attrs={href:h};
var s=el.getAttribute("src");if(s!=null){n.attrs=n.attrs||{};n.attrs.src=s;}
if(el.childNodes.length){
n.children=[];
for(var i=0;ir.json());if(!data.ok)throw new Error(`Telegraph API error: ${data.error}`);
token = data.result.access_token;
GM_setValue('tgphToken', token); console.log("create new telegra.ph token:", token);
return token;
}
async function uploadToTelegraph(title,content){
var telegraphAccessToken=await getTgphToken();
try{const r=await GM.xmlHttpRequest({method:'POST',url:'https://api.telegra.ph/createPage',headers:{'Content-Type':'application/json'},
data:JSON.stringify({access_token:telegraphAccessToken,title:title,content:content,return_content:true,author_name:"DeepSeek Chat"}),responseType:'json',
onerror:(e)=>{throw new Error(`Telegraph API request failed: ${e.status}`)}});
if(r.status>299||r.status<200)throw new Error(`Telegraph API error: ${r.status}`)
const data=r.response; if(!data.ok)throw new Error(`Telegraph API error: ${data.error}`);
return `https://telegra.ph/${data.result.path}`;
}catch(e){console.error("Error uploading to Telegraph:",e);throw e}
}
function deleteTelegraph(postUrl){
var telegraphAccessToken=GM_getValue('tgphToken'); if(!telegraphAccessToken)throw new Error("telegraphAccessToken NOT FOUND, cannot delete the post!");
return fetch(`https://api.telegra.ph/editPage/${postUrl.split('/').pop()}?access_token=${telegraphAccessToken}&title=Removed&author_name=&content=[{"tag":"p","children":["removed by author"]}]`
).then(r=>r.json()).then(j=>{if(!j.ok)throw new Error(j.error);return j;}).catch(e=>{console.error("Error deleting Telegraph:",e);throw e;});
}
// Fetch chat history from DeepSeek API
async function fetchChatHistory() {
const token = localStorage.getItem("userToken");
if (!token) throw new Error("User token not found. Please make sure you are logged in.");
const parsedToken = JSON.parse(token);
// 1. Get current chat session ID.
const sessionId = window.location.pathname.split('/').pop();
if(sessionId.length<30)throw new Error(`Session ID not found from address bar, got: ${sessionId}`);
// 2. Fetch messages using the session ID.
const messagesResponse = await fetch(`https://chat.deepseek.com/api/v0/chat/history_messages?chat_session_id=${sessionId}`,
{credentials: "include",headers:{"Authorization":`Bearer ${parsedToken.value}`}});
if (!messagesResponse.ok) throw new Error(`Failed to fetch chat messages: ${messagesResponse.status}`);
const messagesData = await messagesResponse.json();
return { title: messagesData.data.biz_data.chat_session.title,
messages: messagesData.data.biz_data.chat_messages};
}
// Main function to add the share button.
function addShareButton() {
const titleContainer = document.querySelector(_selectors.titleBar); // The container of title.
if (titleContainer && !document.getElementById('share-chat-button')) {
const shareButton = document.createElement('button');
shareButton.id = 'share-chat-button';
shareButton.textContent = 'Share';
shareButton.style.cssText = `
margin-left: 10px;
padding: 5px 10px;
border-radius: 4px;
`;
titleContainer.appendChild(shareButton);
shareButton.addEventListener('click', async () => {
try {
shareButton.disabled = true;
shareButton.textContent = "Sharing...";
const chatHistory = await fetchChatHistory();
const telegraphContent = convertToTelegraphContent(chatHistory.messages);
const telegraphUrl = await uploadToTelegraph(chatHistory.title, telegraphContent);
showShareOverlay(telegraphUrl);
} catch (error) {
alert(`Error sharing chat: ${error.message}`);
console.error(error);
} finally {
shareButton.disabled = false;
shareButton.textContent = 'Share';
}
});
}
}
// Observe changes in the DOM to add the button when the title bar appears.
const observer = new MutationObserver(()=>addShareButton());
observer.observe(document.body, { childList: true, subtree: true });
// also call addShareButton for first time run
addShareButton();
})();