// ==UserScript== // @name insCNM // @namespace http://tampermonkey.net/ // @version 6.1.1 // @description 获取INS数据! // @author // @match https://www.instagram.com/p/* // @match https://www.instagram.com/reel/* // @match https://www.instagram.com/* // @match https://www.appsheet.com/* // @grant GM_xmlhttpRequest // @grant GM_getResourceText // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_setValue // @grant GM_getValue // @require https://code.jquery.com/jquery-3.6.0.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/toastify-js/1.12.0/toastify.min.js // @resource TOASTIFY_CSS https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css // @icon https://iili.io/29TPCR1.jpg // ==/UserScript== (function() { 'use strict'; // 添加 Toastify 的 CSS 样式 GM_addStyle(GM_getResourceText("TOASTIFY_CSS")); // 默认设置:在页面加载时显示界面 let showOnLoad = GM_getValue('showOnLoad', true); // 更新菜单项的文本 function updateMenuCommand() { GM_registerMenuCommand( (showOnLoad ? '🔴 关闭' : '🟢 打开') + '界面自动显示', toggleShowOnLoad ); } // 切换在页面加载时是否显示界面的函数 function toggleShowOnLoad() { showOnLoad = !showOnLoad; GM_setValue('showOnLoad', showOnLoad); updateMenuCommand(); alert('已' + (showOnLoad ? '开启' : '关闭') + '界面自动显示'); } // 初始化菜单命令 updateMenuCommand(); // 主逻辑 $(document).ready(async function() { // 如果用户设置为在页面加载时显示界面,或者按下了快捷键 if (showOnLoad) { initializeInterface(); } // 监听快捷键 Alt+N 来手动打开或关闭界面 document.addEventListener('keydown', function (e) { if (e.altKey && e.key === 'n') { const container = $('#instagram-fetcher-ui'); if (container.length) { container.css({ transition: 'all 0.5s ease', filter: 'blur(10px)', opacity: '0' }); setTimeout(() => container.remove(), 500); } else { initializeInterface(); } } }); async function initializeInterface() { if (window.location.href.match(/^https:\/\/www\.instagram\.com\/(p|reel)\/[a-zA-Z0-9_-]+\/$/)) { const shortcode = extractShortcodeFromURL(window.location.href); if (shortcode) { const mediaInfo = await fetchMediaInfoByShortcode(shortcode, window.location.href); if (mediaInfo) { const { taken_at, comment_count, like_count } = mediaInfo.items[0]; const mediaInfoHtml = `

发布时间: ${new Date(taken_at * 1000).toLocaleString()}

评论数: ${comment_count}

点赞数: ${like_count}

`; showMediaInfoUI(mediaInfoHtml); } else { showMediaInfoUI('

获取媒体信息失败。

'); } } } else { // 如果不是特定的帖子页面,显示默认界面 showMediaInfoUI('

输入网址后按 Enter 搜索。

'); } } function showMediaInfoUI(mediaInfoHtml) { // 检查是否已经存在菜单界面 if ($('#instagram-fetcher-ui').length) { // 如果存在,更新内容即可 $('#media-info-output').html(mediaInfoHtml); return; } const container = $(`

insCNM

${mediaInfoHtml}
` ); $('body').append(container); $('#instagram-fetcher-ui').animate({ right: '20px' }, 400); // 调用函数使菜单可拖动 dragElement(document.getElementById("instagram-fetcher-ui")); $('#instagram-url-input').on('click', function() { $(this).focus(); }); $('#instagram-url-input').on('focus', function() { $(this).css({ 'box-shadow': '0px 9px 20px rgba(72, 72, 72, 0.3)' }); }); $('#instagram-url-input').on('blur', function() { $(this).css({ 'box-shadow': '0px 8px 15px rgba(72, 72, 72, 0.1)' }); }); $('#instagram-url-input').on('keyup', async function(e) { if (e.which === 13) { // 按下回车键 const url = $('#instagram-url-input').val(); console.log("用户输入的URL:", url); if (url) { const shortcode = extractShortcodeFromURL(url); if (shortcode) { $('#media-info-output').slideUp(200, async function() { $('#media-info-output').text('正在获取媒体信息...'); $(this).slideDown(200); }); const mediaInfo = await fetchMediaInfoByShortcode(shortcode, url); // 传递url if (mediaInfo) { const { taken_at, comment_count, like_count } = mediaInfo.items[0]; $('#media-info-output').slideUp(200, function() { $('#media-info-output').html( `

发布时间: ${new Date(taken_at * 1000).toLocaleString()}

评论数: ${comment_count}

点赞数: ${like_count}

` ); $(this).slideDown(400); }); } else { $('#media-info-output').slideUp(200, function() { $('#media-info-output').text('获取媒体信息失败。').slideDown(400); }); } } else { $('#media-info-output').slideUp(200, function() { $('#media-info-output').text('无效的 Instagram URL。').slideDown(400); }); } } } }); $(document).on('click', '.copy-btn', function() { const textToCopy = $(this).data('copy'); navigator.clipboard.writeText(textToCopy).then(() => { showNotification("已复制到剪贴板"); }).catch(err => { console.error('复制失败:', err); }); }); } function showNotification(message) { Toastify({ text: message, duration: 3000, close: true, gravity: 'top', position: 'center', style: { background: getRandomGradientColor(), color: '#FFFFFF', borderRadius: '2px', }, stopOnFocus: true, }).showToast(); } function getRandomGradientColor() { const gradients = [ 'linear-gradient(to right, rgb(0, 176, 155), rgb(150, 201, 61))', 'linear-gradient(to right, rgb(255,95,109), rgb(255,195,133))', 'linear-gradient(135deg, #73a5ff, #5477fs)' ]; return gradients[Math.floor(Math.random() * gradients.length)]; } function extractShortcodeFromURL(url) { try { const urlObj = new URL(url); const pathSegments = urlObj.pathname.split('/'); console.log("URL 对象:", urlObj); console.log("路径片段:", pathSegments); if (pathSegments[1] === 'p' || pathSegments[1] === 'reel') { return pathSegments[2] ? pathSegments[2] : null; } return null; } catch (error) { console.error("提取短码时出错:", error); return null; } } async function fetchMediaInfoByShortcode(shortcode, inputUrl) { try { // 获取页面内容 const pageContent = await fetchPageContent(inputUrl); // 从页面内容中提取 APP_ID 和 media_id const appId = getAppID(pageContent); const mediaId = getMediaIDFromContent(pageContent); if (!appId || !mediaId) { console.error("获取 APP_ID 或 Media ID 失败。"); $('#media-info-output').text('获取 APP_ID 或 Media ID 失败,请稍后重试。'); return null; } const mediaInfo = await getMediaInfo(mediaId, appId); console.log("媒体信息:", mediaInfo); return mediaInfo; } catch (error) { console.error("检索媒体信息时出错:", error); return null; } } // 获取页面内容 async function fetchPageContent(url) { return new Promise((resolve, reject) => { GM_xmlhttpRequest({ method: "GET", url: url, headers: { "User-Agent": window.navigator.userAgent, "Accept": "text/html" }, onload: function(response) { if (response.status === 200) { resolve(response.responseText); } else { reject("获取页面内容失败。"); } }, onerror: function(err) { reject(err); } }); }); } // 获取 APP_ID function getAppID(pageContent) { const regex = /"APP_ID":"([0-9]+)"/i; const match = pageContent.match(regex); if (match && match[1]) { return match[1]; } return null; } // 获取 media_id function getMediaIDFromContent(pageContent) { const regex = /"media_id":"(\d+)"/; const match = pageContent.match(regex); if (match && match[1]) { return match[1]; } return null; } // 获取媒体信息 async function getMediaInfo(mediaId, appId) { return new Promise((resolve, reject) => { let getURL = `https://i.instagram.com/api/v1/media/${mediaId}/info/`; if (mediaId == null) { console.error("由于媒体 ID 无效,无法调用媒体 API。"); reject("由于媒体 ID 无效,无法调用媒体 API。"); return; } GM_xmlhttpRequest({ method: "GET", url: getURL, headers: { "User-Agent": window.navigator.userAgent, "Accept": "application/json", 'X-IG-App-ID': appId }, onload: function (response) { try { if (response.finalUrl == getURL) { let obj = JSON.parse(response.responseText); resolve(obj); } else { let finalURL = new URL(response.finalUrl); if (finalURL.pathname.startsWith('/accounts/login')) { console.error("必须登录账户才能访问媒体 API。"); } else { console.error('无法检索内容,因为 API 被重定向到 "' + response.finalUrl + '"'); } reject(-1); } } catch (error) { console.error("解析 JSON 响应时出错:", error); reject(error); } }, onerror: function (err) { reject(err); } }); }); } // 使元素可拖动的函数 function dragElement(element) { let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0; if (element) { element.onmousedown = dragMouseDown; } function dragMouseDown(e) { e = e || window.event; e.preventDefault(); // 获取鼠标的初始位置 pos3 = e.clientX; pos4 = e.clientY; document.onmouseup = closeDragElement; document.onmousemove = elementDrag; } function elementDrag(e) { e = e || window.event; e.preventDefault(); // 计算鼠标移动的距离 pos1 = pos3 - e.clientX; pos2 = pos4 - e.clientY; pos3 = e.clientX; pos4 = e.clientY; // 设置元素的新位置 element.style.top = (element.offsetTop - pos2) + "px"; element.style.left = (element.offsetLeft - pos1) + "px"; } function closeDragElement() { // 停止拖动时清除事件监听 document.onmouseup = null; document.onmousemove = null; } } }); })();