(function () {
let hasInitialized = false;
const showPandaWiki = localStorage.getItem('show-panda-wiki') || '';
const positionStorage = localStorage.getItem('panda-wiki-position') || '';
const [left, top] = positionStorage.split(',');
const script = document.currentScript.src;
const origin = new URL(script).origin;
const link = new URL(script).searchParams.get('link');
const tools = new URL(script).searchParams.get('tools');
const makeDraggable = (element, icon) => {
let isDragging = false;
let startX, startY, initialX, initialY;
let animationFrameId = null;
let dragTimer = null;
const onMouseDown = (e) => {
startX = e.clientX;
startY = e.clientY;
const rect = element.getBoundingClientRect();
initialX = rect.left;
initialY = rect.top;
// 设置0.5秒的定时器,延迟设置拖拽状态
dragTimer = setTimeout(() => {
isDragging = true;
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
}, 500);
document.addEventListener('mouseup', onMouseUp);
};
const onMouseMove = (e) => {
if (!isDragging) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
animationFrameId = requestAnimationFrame(() => {
element.style.left = `${initialX + dx}px`;
element.style.top = `${initialY + dy}px`;
localStorage.setItem('panda-wiki-position', `${initialX + dx}px,${initialY + dy}px`);
});
};
const onMouseUp = () => {
// 清除定时器
if (dragTimer) {
clearTimeout(dragTimer);
dragTimer = null;
}
// 如果没有进入拖拽状态,则不执行任何操作
if (!isDragging) {
document.removeEventListener('mouseup', onMouseUp);
return;
}
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
};
icon.addEventListener('click', (e) => {
if (isDragging) {
e.stopPropagation();
} else {
isDragging = false;
}
});
icon.addEventListener('mousedown', onMouseDown);
};
const createWidget = (element) => {
const widget = document.createElement('div');
widget.className = 'panda-wiki-widget';
const search_text = document.createElement('div');
search_text.className = 'panda-wiki-search';
search_text.innerHTML = '开始搜索您的问题';
widget.appendChild(search_text);
element.appendChild(widget);
const ai_text = document.createElement('div');
ai_text.className = 'panda-wiki-text';
ai_text.innerHTML = 'AI 小助手';
element.appendChild(ai_text);
}
const createLogo = (element) => {
const icon = document.createElement('div');
icon.className = 'panda-wiki-icon';
icon.innerHTML = `
`;
element.appendChild(icon);
makeDraggable(element, icon);
}
const createHideModal = (element) => {
const hideModal = document.createElement('div');
hideModal.className = 'panda-wiki-hide-modal';
const hideContainer = document.createElement('div');
hideContainer.className = 'panda-wiki-hide-container';
hideContainer.innerHTML = ``;
const hideBody = document.createElement('div');
hideBody.className = 'panda-wiki-hide-body';
const option1 = document.createElement('div');
option1.className = 'panda-wiki-hide-option';
const radio1 = document.createElement('input');
radio1.type = 'radio';
radio1.name = 'panda-wiki-hide-radio';
radio1.id = 'panda-wiki-hide-radio-one';
radio1.value = 'one';
radio1.checked = true;
option1.appendChild(radio1);
const label1 = document.createElement('label');
label1.htmlFor = 'panda-wiki-hide-radio-one';
label1.innerHTML = '隐藏本次 将在下次刷新页面时展示并复位挂件';
option1.appendChild(label1);
hideBody.appendChild(option1);
const option2 = document.createElement('div');
option2.className = 'panda-wiki-hide-option';
const radio2 = document.createElement('input');
radio2.type = 'radio';
radio2.name = 'panda-wiki-hide-radio';
radio2.value = 'one-week';
radio2.id = 'panda-wiki-hide-radio-one-week';
option2.appendChild(radio2);
const label2 = document.createElement('label');
label2.htmlFor = 'panda-wiki-hide-radio-one-week';
label2.innerHTML = '隐藏 7 天 7 天后展示并复位挂件';
option2.appendChild(label2);
hideBody.appendChild(option2);
hideContainer.appendChild(hideBody);
const closeIconBtn = document.createElement('div');
closeIconBtn.className = 'panda-wiki-hide-modal-icon';
closeIconBtn.innerHTML = ''
hideContainer.appendChild(closeIconBtn);
closeIconBtn.addEventListener('click', () => {
hideModal.classList.remove('active');
})
const hideFooter = document.createElement('div');
hideFooter.className = 'panda-wiki-hide-footer';
hideContainer.appendChild(hideFooter);
const cancelBtn = document.createElement('button');
cancelBtn.className = 'panda-wiki-hide-cancel-btn';
cancelBtn.innerHTML = '取消';
hideFooter.appendChild(cancelBtn);
const confirmBtn = document.createElement('button');
confirmBtn.className = 'panda-wiki-hide-confirm-btn';
confirmBtn.innerHTML = '确认';
hideFooter.appendChild(confirmBtn);
hideModal.appendChild(hideContainer);
document.body.appendChild(hideModal);
cancelBtn.addEventListener('click', () => {
hideModal.classList.remove('active');
})
confirmBtn.addEventListener('click', () => {
const selectedOption = document.querySelector('input[name="panda-wiki-hide-radio"]:checked').value
if (selectedOption === 'one-week') {
localStorage.setItem('show-panda-wiki', Date.now() + 7 * 24 * 60 * 60 * 1000);
}
localStorage.removeItem('panda-wiki-position');
hideModal.classList.remove('active');
element.style.display = 'none';
})
hideModal.addEventListener('click', (e) => {
if (e.target === hideModal) {
hideModal.classList.remove('active');
}
});
const closeIcon = document.createElement('div');
closeIcon.className = 'panda-wiki-hide-btn';
closeIcon.innerHTML = ''
element.appendChild(closeIcon);
closeIcon.addEventListener('click', (event) => {
event.stopPropagation();
hideModal.classList.add('active');
})
}
const createIframe = (element) => {
const modal = document.createElement('div');
modal.className = 'panda-wiki-modal';
const iframeContainer = document.createElement('div');
iframeContainer.className = 'panda-wiki-iframe-container';
const closeBtn = document.createElement('div');
closeBtn.className = 'panda-wiki-modal-close';
closeBtn.innerHTML = ''
iframeContainer.appendChild(closeBtn);
const iframe = document.createElement('iframe');
iframe.className = 'panda-wiki-iframe';
iframe.src = `${origin}/plugin/${link}?tools=${tools}`
element.addEventListener('click', () => {
iframeContainer.appendChild(iframe);
modal.classList.add('active');
});
closeBtn.addEventListener('click', () => {
iframeContainer.removeChild(iframe);
modal.classList.remove('active');
});
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('active');
}
});
modal.appendChild(iframeContainer);
document.body.appendChild(modal);
}
const init = () => {
if (hasInitialized) return;
hasInitialized = true;
const container = document.createElement('div');
container.className = 'panda-wiki-container';
if (showPandaWiki && Date.now() < showPandaWiki) {
return
}
if (link) {
fetch(`${origin}/share/v1/app/link?link=${link}`).then(res => {
if (res.ok) {
res.json().then(data => {
const position = data?.data?.settings?.position || [4, 24, 24];
switch (position[0]) {
case 1:
container.style.top = position[1] + 'px'
container.style.left = position[2] + 'px'
break;
case 2:
container.style.top = position[1] + 'px'
container.style.right = position[2] + 'px'
break;
case 3:
container.style.bottom = position[1] + 'px'
container.style.left = position[2] + 'px'
break;
case 5:
container.style.top = 'calc(50% - 34px)'
container.style.left = position[2] + 'px'
break;
case 6:
container.style.top = 'calc(50% - 34px)'
container.style.right = position[2] + 'px'
break;
default:
container.style.bottom = position[1] + 'px'
container.style.right = position[2] + 'px'
}
if (positionStorage) {
container.style.left = left
container.style.top = top
}
container.style.display = 'block';
})
}
})
}
createWidget(container);
createLogo(container);
createHideModal(container);
createIframe(container);
document.body.appendChild(container);
}
if (document.readyState === 'complete') init();
else if (document.readyState === 'interactive') {
document.addEventListener('DOMContentLoaded', init, { once: true });
} else {
document.addEventListener('DOMContentLoaded', init, { once: true });
}
window.addEventListener('load', init, { once: true });
})();