// ==UserScript==
// @name Pinterest Ultra Assistant V6.7 (Instant Fix)
// @namespace http://tampermonkey.net/
// @version 6.7
// @description Instant Injection: Works on slow networks & detail pages. One-Click Originals, AI 2x Sharpen, and Source Finder.
// @author Pi Xiao
// @match https://*.pinterest.com/*
// @grant GM_openInTab
// @grant GM_setClipboard
// @license MIT
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
const BRIDGE_PAGE = "https://meishubiji.cn/ai-processing-center/";
// --- 核心逻辑 ---
const getOriginalUrl = (url) => url.replace(/\/(236x|474x|564x|736x|1200x)\//, '/originals/').replace(/\.webp$/, '.jpg');
async function processAndShow(imgUrl) {
const originalUrl = getOriginalUrl(imgUrl);
const overlay = document.createElement('div');
overlay.style = "position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.96);z-index:2147483647;display:flex;flex-direction:column;align-items:center;justify-content:center;color:white;font-family:sans-serif;cursor:zoom-out;";
overlay.innerHTML = '
';
overlay.onclick = () => overlay.remove();
document.body.appendChild(overlay);
const img = new Image();
img.crossOrigin = "Anonymous";
img.src = originalUrl;
img.onload = function() {
overlay.innerHTML = "";
const container = document.createElement('div');
container.style = "text-align:center;width:95%;height:90vh;display:flex;flex-direction:column;cursor:default;";
container.onclick = (e) => e.stopPropagation();
const scrollBox = document.createElement('div');
scrollBox.style = "overflow:auto;border:1px solid #333;border-radius:12px;flex:1;background:#050505;display:flex;align-items:center;justify-content:center;";
const pImg = document.createElement('img');
pImg.src = originalUrl;
pImg.style = "max-width:200%;image-rendering:-webkit-optimize-contrast;filter:contrast(1.05);";
scrollBox.appendChild(pImg);
const btnAi = document.createElement('button');
btnAi.innerHTML = '🚀 LAUNCH AI 8K ENGINE';
btnAi.style = "background:linear-gradient(45deg,#6a11cb 0%,#2575fc 100%);color:white;border:none;padding:12px 40px;border-radius:50px;font-size:16px;font-weight:bold;cursor:pointer;margin:20px;";
btnAi.onclick = () => window.open(`${BRIDGE_PAGE}?url=${encodeURIComponent(originalUrl)}`, '_blank');
container.appendChild(scrollBox);
container.appendChild(btnAi);
overlay.appendChild(container);
};
img.onerror = () => { window.open(originalUrl, '_blank'); overlay.remove(); };
}
// --- 3. 增强版注入逻辑 ---
function injectButtons() {
const images = document.querySelectorAll('img[src*="pinimg.com"]:not(.px-done)');
images.forEach(img => {
if (img.width < 100) return;
img.classList.add('px-done');
// 详情页和首页通用的容器寻找逻辑
const container = img.closest('[data-test-id="visual-content-container"]') ||
img.closest('[data-test-id="pin-visual-wrapper"]') ||
img.parentElement;
if (container) {
// 强制确保容器可以相对定位
if (window.getComputedStyle(container).position === 'static') {
container.style.setProperty('position', 'relative', 'important');
}
const bar = document.createElement('div');
bar.className = 'px-helper-bar';
// 详情页调大一点,首页保持精致
const isDetail = location.href.includes('/pin/');
bar.style = `position:absolute; top:${isDetail ? '20px' : '10px'}; left:${isDetail ? '20px' : '10px'}; z-index:2147483640; display:flex; gap:5px; opacity:0; transition:opacity 0.3s; pointer-events:auto;`;
const btnStyle = 'color:white; border:none; border-radius:4px; cursor:pointer; padding:5px 8px; font-weight:bold; font-size:10px; box-shadow:0 2px 8px rgba(0,0,0,0.5); white-space:nowrap;';
const b1 = document.createElement('button'); b1.innerHTML = '🪄 2x HD'; b1.style = btnStyle + 'background:#00BFFF;';
b1.onclick = (e) => { e.preventDefault(); e.stopPropagation(); processAndShow(img.src); };
const b2 = document.createElement('button'); b2.innerHTML = '🖼️ Originals'; b2.style = btnStyle + 'background:#E60023;';
b2.onclick = (e) => { e.preventDefault(); e.stopPropagation(); window.open(getOriginalUrl(img.src), '_blank'); };
const b3 = document.createElement('button'); b3.innerHTML = '🔍 Source'; b3.style = btnStyle + 'background:#34a853;';
b3.onclick = (e) => { e.preventDefault(); e.stopPropagation(); window.open(`https://lens.google.com/uploadbyurl?url=${encodeURIComponent(getOriginalUrl(img.src))}`, '_blank'); };
bar.append(b1, b2, b3);
container.appendChild(bar);
container.addEventListener('mouseenter', () => bar.style.opacity = "1");
container.addEventListener('mouseleave', () => bar.style.opacity = "0");
}
});
}
// --- 启动逻辑:不再等待 window.load ---
// 采用立即运行 + 持续监听模式
injectButtons();
const observer = new MutationObserver(injectButtons);
observer.observe(document.body, { childList: true, subtree: true });
// 每 2 秒全量补漏一次,应对极端网络环境
setInterval(injectButtons, 2000);
})();