MediaWiki:Gadget-blamefinder.js: различия между версиями
Ланселот (обсуждение | вклад) Нет описания правки |
Ланселот (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
/** | /** | ||
* Gadget: Blame Finder | * Gadget: Blame Finder | ||
* Версия 2.0 с поддержкой ночного режима | * Версия 2.0 с поддержкой ночного режима и петаскиванием кнопки | ||
* Назначение: поиск автора любого текста в статье | * Назначение: поиск автора любого текста в статье | ||
* Локализация под wiki.peresvet.artushak.ru, лицензия: CC BY-SA 4.0 | * Локализация под wiki.peresvet.artushak.ru, лицензия: CC BY-SA 4.0 | ||
| Строка 38: | Строка 38: | ||
scriptPath = '/w'; | scriptPath = '/w'; | ||
} | } | ||
var BUTTON_STORAGE_KEY = 'blameFinderButtonPos'; | |||
var LOG_WIDTH = 380; | |||
var LOG_MAX_HEIGHT_CSS = 'min(300px, 50vh)'; | |||
function escapeText(text, escapeSlash) { | function escapeText(text, escapeSlash) { | ||
| Строка 65: | Строка 69: | ||
var trimmed = url.trim(); | var trimmed = url.trim(); | ||
if (trimmed === '') return null; | if (trimmed === '') return null; | ||
if (trimmed.charAt(0) === '/' || trimmed.charAt(0) === '#') { | if (trimmed.charAt(0) === '/' || trimmed.charAt(0) === '#') { | ||
return trimmed; | return trimmed; | ||
} | } | ||
try { | try { | ||
var parsed = new URL(trimmed, window.location.origin); | var parsed = new URL(trimmed, window.location.origin); | ||
| Строка 76: | Строка 77: | ||
return trimmed; | return trimmed; | ||
} | } | ||
} catch (e) { | } catch (e) {} | ||
return null; | return null; | ||
} | } | ||
| Строка 83: | Строка 83: | ||
function createSafeElement(tag, options) { | function createSafeElement(tag, options) { | ||
var element = document.createElement(tag); | var element = document.createElement(tag); | ||
if (!options) return element; | if (!options) return element; | ||
if (options.className) element.className = options.className; | if (options.className) element.className = options.className; | ||
if (options.id) element.id = options.id; | if (options.id) element.id = options.id; | ||
if (options.text) element.textContent = options.text; | if (options.text) element.textContent = options.text; | ||
if (options.html) | if (options.html) element.textContent = options.html; | ||
if (options.css) { | if (options.css) { | ||
| Строка 103: | Строка 99: | ||
if (key === 'href' || key === 'src') { | if (key === 'href' || key === 'src') { | ||
var safe = sanitizeUrl(options.attrs[key]); | var safe = sanitizeUrl(options.attrs[key]); | ||
if (safe !== null) | if (safe !== null) element.setAttribute(key, safe); | ||
} else { | } else { | ||
element.setAttribute(key, options.attrs[key]); | element.setAttribute(key, options.attrs[key]); | ||
| Строка 111: | Строка 105: | ||
}); | }); | ||
} | } | ||
return element; | return element; | ||
} | } | ||
| Строка 117: | Строка 110: | ||
function createSafeLink(url, text, title) { | function createSafeLink(url, text, title) { | ||
var safeUrl = sanitizeUrl(url) || '#'; | var safeUrl = sanitizeUrl(url) || '#'; | ||
return createSafeElement('a', { | |||
text: text, | text: text, | ||
attrs: { | attrs: { | ||
| Строка 127: | Строка 119: | ||
} | } | ||
}); | }); | ||
} | } | ||
| Строка 141: | Строка 131: | ||
var html = document.documentElement; | var html = document.documentElement; | ||
var body = document.body; | var body = document.body; | ||
if (html.classList.contains('skin-theme-clientpref-night')) return true; | if (html.classList.contains('skin-theme-clientpref-night')) return true; | ||
if (html.classList.contains('skin-theme-clientpref-day')) return false; | if (html.classList.contains('skin-theme-clientpref-day')) return false; | ||
| Строка 147: | Строка 136: | ||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
} | } | ||
var dataTheme = (html.getAttribute('data-theme') || '') + ' ' + | var dataTheme = (html.getAttribute('data-theme') || '') + ' ' + | ||
(body ? (body.getAttribute('data-theme') || '') : ''); | (body ? (body.getAttribute('data-theme') || '') : ''); | ||
if (/dark|night/i.test(dataTheme)) return true; | if (/dark|night/i.test(dataTheme)) return true; | ||
if (/light|day/i.test(dataTheme)) return false; | if (/light|day/i.test(dataTheme)) return false; | ||
if (body) { | if (body) { | ||
var bg = window.getComputedStyle(body).backgroundColor; | var bg = window.getComputedStyle(body).backgroundColor; | ||
| Строка 173: | Строка 160: | ||
var namespace = 0; | var namespace = 0; | ||
var isSpecial = false; | var isSpecial = false; | ||
var pathname = window.location.pathname || ''; | var pathname = window.location.pathname || ''; | ||
var search = window.location.search || ''; | var search = window.location.search || ''; | ||
| Строка 193: | Строка 179: | ||
if (candidate.indexOf(':') !== -1) { | if (candidate.indexOf(':') !== -1) { | ||
var prefix = candidate.split(':')[0].toLowerCase(); | var prefix = candidate.split(':')[0].toLowerCase(); | ||
if (excludedNamespaces.indexOf(prefix) !== -1) | if (excludedNamespaces.indexOf(prefix) !== -1) namespace = 1; | ||
if (prefix === 'special' || prefix === 'служебная') isSpecial = true; | |||
if (prefix === 'special' || prefix === 'служебная') | |||
} | } | ||
} | } | ||
| Строка 205: | Строка 187: | ||
var urlParams = new URLSearchParams(search); | var urlParams = new URLSearchParams(search); | ||
var paramTitle = urlParams.get('title'); | var paramTitle = urlParams.get('title'); | ||
if (paramTitle) { | if (paramTitle) { | ||
paramTitle = validateTitle(paramTitle); | paramTitle = validateTitle(paramTitle); | ||
| Строка 215: | Строка 196: | ||
if (!title) { | if (!title) { | ||
if (/special:|служебная:/i.test(pathname)) | if (/special:|служебная:/i.test(pathname)) isSpecial = true; | ||
if (pathname.indexOf('/wiki/') !== -1) { | if (pathname.indexOf('/wiki/') !== -1) { | ||
var pathTitle = pathname.split('/wiki/')[1]; | var pathTitle = pathname.split('/wiki/')[1]; | ||
if (pathTitle) { | if (pathTitle) { | ||
try { | try { pathTitle = decodeURIComponent(pathTitle); } catch (e) {} | ||
pathTitle = pathTitle.split('?')[0].split('#')[0]; | pathTitle = pathTitle.split('?')[0].split('#')[0]; | ||
classifyTitle(pathTitle); | classifyTitle(pathTitle); | ||
title = validateTitle(pathTitle); | title = validateTitle(pathTitle); | ||
} | } | ||
} else { | } else { | ||
var segments = pathname.split('/').filter(function(s) { | var segments = pathname.split('/').filter(function(s) { return s.length > 0; }); | ||
if (segments.length > 0) { | if (segments.length > 0) { | ||
var rootTitle = segments[segments.length - 1]; | var rootTitle = segments[segments.length - 1]; | ||
try { | try { rootTitle = decodeURIComponent(rootTitle); } catch (e) {} | ||
rootTitle = rootTitle.split('?')[0].split('#')[0]; | rootTitle = rootTitle.split('?')[0].split('#')[0]; | ||
classifyTitle(rootTitle); | classifyTitle(rootTitle); | ||
title = validateTitle(rootTitle); | title = validateTitle(rootTitle); | ||
| Строка 252: | Строка 220: | ||
if (!title) { | if (!title) { | ||
var h1 = document.querySelector('h1.firstHeading, .firstHeading, #firstHeading'); | var h1 = document.querySelector('h1.firstHeading, .firstHeading, #firstHeading'); | ||
if (h1 && h1.textContent) | if (h1 && h1.textContent) title = validateTitle(h1.textContent.trim()); | ||
} | } | ||
} catch (e) { | } catch (e) { | ||
| Строка 260: | Строка 226: | ||
} | } | ||
return { | return { title: title || 'Main_Page', isArticle: namespace === 0 && !isSpecial }; | ||
} | } | ||
| Строка 303: | Строка 266: | ||
xhr.onreadystatechange = function() { | xhr.onreadystatechange = function() { | ||
if (completed) return; | if (completed) return; | ||
if (xhr.readyState === 4) { | if (xhr.readyState === 4) { | ||
if (xhr.status === 200) { | if (xhr.status === 200) { | ||
| Строка 315: | Строка 277: | ||
return; | return; | ||
} | } | ||
try { | try { | ||
var data = JSON.parse(xhr.responseText); | var data = JSON.parse(xhr.responseText); | ||
if (!data || typeof data !== 'object') | if (!data || typeof data !== 'object') throw new Error('Invalid response format'); | ||
clearTimeout(timeoutId); | clearTimeout(timeoutId); | ||
completed = true; | completed = true; | ||
| Строка 351: | Строка 310: | ||
} | } | ||
// ===== ЛОГ-ПАНЕЛЬ ===== | |||
var logPanel = null; | var logPanel = null; | ||
var logEntries = []; | var logEntries = []; | ||
var logVisible = false; | |||
function createLogPanel() { | function createLogPanel() { | ||
| Строка 361: | Строка 322: | ||
css: { | css: { | ||
position: 'fixed', | position: 'fixed', | ||
bottom: ' | bottom: '80px', | ||
right: '20px', | |||
width: ' | width: LOG_WIDTH + 'px', | ||
maxHeight: | maxHeight: LOG_MAX_HEIGHT_CSS, | ||
overflowY: 'auto', | overflowY: 'auto', | ||
background: 'rgba(0,0,0,0.9)', | background: 'rgba(0,0,0,0.9)', | ||
| Строка 391: | Строка 352: | ||
var titleSpan = createSafeElement('span', { | var titleSpan = createSafeElement('span', { | ||
text: 'Логи BlameFinder', | text: 'Логи BlameFinder', | ||
css: { | css: { color: '#36c', fontWeight: 'bold' } | ||
}); | }); | ||
| Строка 440: | Строка 398: | ||
var content = createSafeElement('div', { | var content = createSafeElement('div', { | ||
id: 'blame-log-content', | id: 'blame-log-content', | ||
css: { | css: { wordBreak: 'break-word' } | ||
}); | }); | ||
| Строка 455: | Строка 411: | ||
}; | }; | ||
toggleButton.onclick = function(e) { | toggleButton.onclick = function(e) { | ||
e.stopPropagation(); | e.stopPropagation(); | ||
| Строка 461: | Строка 416: | ||
logPanel.style.display = logVisible ? 'block' : 'none'; | logPanel.style.display = logVisible ? 'block' : 'none'; | ||
toggleButton.textContent = logVisible ? 'Скрыть' : 'Показать'; | toggleButton.textContent = logVisible ? 'Скрыть' : 'Показать'; | ||
syncLogToggleLabel(); | |||
}; | }; | ||
logPanel.style.display = 'none'; | |||
} | |||
function showLogPanel() { | |||
if (!logPanel) return; | |||
if (logVisible) return; | |||
logVisible = true; | |||
logPanel.style.display = 'block'; | logPanel.style.display = 'block'; | ||
syncLogToggleLabel(); | |||
positionLogPanelNearButton(); | |||
} | |||
function syncLogToggleLabel() { | |||
var toggle = document.getElementById('blame-toggle'); | |||
if (toggle) toggle.textContent = logVisible ? 'Скрыть' : 'Показать'; | |||
} | |||
function positionLogPanelNearButton() { | |||
if (!logPanel) return; | |||
var btn = document.getElementById('blame-finder-button'); | |||
if (!btn) return; | |||
var r = btn.getBoundingClientRect(); | |||
var panelW = Math.min(LOG_WIDTH, window.innerWidth - 20); | |||
var panelH = Math.min(300, window.innerHeight * 0.5); | |||
var left = r.left; | |||
if (left + panelW > window.innerWidth - 10) { | |||
left = Math.max(10, window.innerWidth - panelW - 10); | |||
} | |||
var top = r.top - panelH - 10; | |||
if (top < 10) { | |||
top = Math.min(r.bottom + 10, window.innerHeight - panelH - 10); | |||
} | |||
logPanel.style.left = left + 'px'; | |||
logPanel.style.top = top + 'px'; | |||
logPanel.style.right = 'auto'; | |||
logPanel.style.bottom = 'auto'; | |||
logPanel.style.width = panelW + 'px'; | |||
} | } | ||
function addLog(message, type) { | function addLog(message, type) { | ||
var timestamp = new Date().toLocaleTimeString('ru', { | var timestamp = new Date().toLocaleTimeString('ru', { timeZoneName: 'short' }); | ||
var color = '#0f0'; | var color = '#0f0'; | ||
if (type === 'error') color = '#f00'; | if (type === 'error') color = '#f00'; | ||
| Строка 476: | Строка 470: | ||
var safeMessage = escapeForLog(String(message)); | var safeMessage = escapeForLog(String(message)); | ||
logEntries.unshift({ time: timestamp, text: safeMessage, color: color }); | |||
logEntries.unshift({ | |||
if (logEntries.length > 30) logEntries.pop(); | if (logEntries.length > 30) logEntries.pop(); | ||
updateLogDisplay(); | updateLogDisplay(); | ||
| Строка 490: | Строка 478: | ||
var content = document.getElementById('blame-log-content'); | var content = document.getElementById('blame-log-content'); | ||
if (!content) return; | if (!content) return; | ||
while (content.firstChild) content.removeChild(content.firstChild); | |||
while (content.firstChild) | |||
logEntries.forEach(function(entry) { | logEntries.forEach(function(entry) { | ||
var entryDiv = createSafeElement('div', { | var entryDiv = createSafeElement('div', { | ||
css: { | css: { color: entry.color, marginBottom: '3px' } | ||
}); | }); | ||
entryDiv.textContent = '[' + entry.time + '] ' + entry.text; | entryDiv.textContent = '[' + entry.time + '] ' + entry.text; | ||
content.appendChild(entryDiv); | content.appendChild(entryDiv); | ||
}); | }); | ||
} | |||
// ===== КНОПКА + DRAG ===== | |||
function clamp(v, min, max) { | |||
return Math.max(min, Math.min(max, v)); | |||
} | } | ||
function addButton() { | function addButton() { | ||
addLog('Инициализация BlameFinder для wiki | if (document.getElementById('blame-finder-button')) return; | ||
addLog('Инициализация BlameFinder для absurdopedia.wiki', 'info'); | |||
var button = createSafeElement('div', { | var button = createSafeElement('div', { | ||
| Строка 516: | Строка 504: | ||
css: { | css: { | ||
position: 'fixed', | position: 'fixed', | ||
right: '20px', | |||
bottom: '20px', | bottom: '20px', | ||
background: '#36c', | background: '#36c', | ||
color: 'white', | color: 'white', | ||
padding: '10px 20px', | padding: '10px 20px', | ||
borderRadius: '5px', | borderRadius: '5px', | ||
cursor: ' | cursor: 'grab', | ||
zIndex: '9999', | zIndex: '9999', | ||
fontFamily: 'sans-serif', | fontFamily: 'sans-serif', | ||
fontSize: '14px', | fontSize: '14px', | ||
boxShadow: '0 2px 10px rgba(0,0,0,0.3)' | boxShadow: '0 2px 10px rgba(0,0,0,0.3)', | ||
userSelect: 'none', | |||
webkitUserSelect: 'none', | |||
touchAction: 'none', | |||
transition: 'background 0.15s' | |||
} | } | ||
}); | }); | ||
button. | document.body.appendChild(button); | ||
createLogPanel(); | |||
}; | restoreButtonPosition(button); | ||
makeButtonDraggable(button); | |||
addLog('Кнопка добавлена', 'success'); | |||
} | |||
function restoreButtonPosition(button) { | |||
var saved = null; | |||
try { | |||
saved = JSON.parse(localStorage.getItem(BUTTON_STORAGE_KEY) || 'null'); | |||
} catch (e) { | |||
saved = null; | |||
} | |||
if (!saved || typeof saved.left !== 'number' || typeof saved.top !== 'number') return; | |||
var rect = button.getBoundingClientRect(); | |||
var w = rect.width || 120; | |||
var h = rect.height || 40; | |||
var left = clamp(saved.left, 0, window.innerWidth - w); | |||
var top = clamp(saved.top, 0, window.innerHeight - h); | |||
button.style.right = 'auto'; | |||
button.style.bottom = 'auto'; | |||
button.style.left = left + 'px'; | |||
button.style.top = top + 'px'; | |||
} | |||
function saveButtonPosition(button) { | |||
var rect = button.getBoundingClientRect(); | |||
try { | |||
localStorage.setItem(BUTTON_STORAGE_KEY, JSON.stringify({ | |||
left: Math.round(rect.left), | |||
top: Math.round(rect.top) | |||
})); | |||
} catch (e) {} | |||
} | |||
function makeButtonDraggable(button) { | |||
var startX = 0, startY = 0, startLeft = 0, startTop = 0; | |||
var pointerId = null; | |||
var moved = false; | |||
var MOVE_THRESHOLD = 5; | |||
function onPointerDown(e) { | |||
if (e.button !== undefined && e.button !== 0) return; | |||
pointerId = e.pointerId; | |||
moved = false; | |||
var rect = button.getBoundingClientRect(); | |||
startLeft = rect.left; | |||
startTop = rect.top; | |||
startX = e.clientX; | |||
startY = e.clientY; | |||
button.style.right = 'auto'; | |||
button.style.bottom = 'auto'; | |||
button.style.left = startLeft + 'px'; | |||
button.style.top = startTop + 'px'; | |||
button.style.cursor = 'grabbing'; | |||
button.dataset.dragging = '1'; | |||
try { button.setPointerCapture(pointerId); } catch (err) {} | |||
button.addEventListener('pointermove', onPointerMove); | |||
button.addEventListener('pointerup', onPointerUp); | |||
button.addEventListener('pointercancel', onPointerUp); | |||
} | |||
function onPointerMove(e) { | |||
if (e.pointerId !== pointerId) return; | |||
var dx = e.clientX - startX; | |||
var dy = e.clientY - startY; | |||
if (!moved && (Math.abs(dx) > MOVE_THRESHOLD || Math.abs(dy) > MOVE_THRESHOLD)) { | |||
moved = true; | |||
} | |||
if (!moved) return; | |||
var rect = button.getBoundingClientRect(); | |||
var w = rect.width, h = rect.height; | |||
var newLeft = clamp(startLeft + dx, 0, window.innerWidth - w); | |||
var newTop = clamp(startTop + dy, 0, window.innerHeight - h); | |||
button.style.left = newLeft + 'px'; | |||
button.style.top = newTop + 'px'; | |||
if (logVisible) positionLogPanelNearButton(); | |||
e.preventDefault(); | |||
} | |||
function onPointerUp(e) { | |||
if (e.pointerId !== pointerId) return; | |||
try { button.releasePointerCapture(pointerId); } catch (err) {} | |||
button.removeEventListener('pointermove', onPointerMove); | |||
button.removeEventListener('pointerup', onPointerUp); | |||
button.removeEventListener('pointercancel', onPointerUp); | |||
button.style.cursor = 'grab'; | |||
delete button.dataset.dragging; | |||
if (moved) saveButtonPosition(button); | |||
pointerId = null; | |||
} | |||
button.addEventListener('pointerdown', onPointerDown); | |||
button. | button.addEventListener('mouseenter', function() { | ||
if (!button.dataset.dragging) button.style.background = '#447ff5'; | |||
}; | }); | ||
button.addEventListener('mouseleave', function() { | |||
if (!button.dataset.dragging) button.style.background = '#36c'; | |||
}); | |||
button.addEventListener('click', function(e) { | button.addEventListener('click', function(e) { | ||
if (moved) { | |||
moved = false; | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
return; | |||
} | |||
e.preventDefault(); | e.preventDefault(); | ||
showLogPanel(); | |||
activateSelectionMode(); | activateSelectionMode(); | ||
}); | }); | ||
button.addEventListener('dblclick', function(e) { | |||
e.preventDefault(); | |||
e.stopPropagation(); | |||
try { localStorage.removeItem(BUTTON_STORAGE_KEY); } catch (err) {} | |||
button.style.left = 'auto'; | |||
button.style.top = 'auto'; | |||
button.style.right = '20px'; | |||
button.style.bottom = '20px'; | |||
if (logVisible) positionLogPanelNearButton(); | |||
}); | |||
window.addEventListener('resize', function() { | |||
var rect = button.getBoundingClientRect(); | |||
if (rect.left + rect.width > window.innerWidth || | |||
rect.top + rect.height > window.innerHeight) { | |||
button.style.left = clamp(rect.left, 0, window.innerWidth - rect.width) + 'px'; | |||
button.style.top = clamp(rect.top, 0, window.innerHeight - rect.height) + 'px'; | |||
} | |||
if (logVisible) positionLogPanelNearButton(); | |||
}); | |||
} | } | ||
// ===== РЕЖИМ ВЫДЕЛЕНИЯ ===== | |||
var selectionActive = false; | var selectionActive = false; | ||
var selectionTimer = null; | var selectionTimer = null; | ||
| Строка 583: | Строка 703: | ||
tooltip.appendChild(document.createElement('br')); | tooltip.appendChild(document.createElement('br')); | ||
var small = createSafeElement('small', { | var small = createSafeElement('small', { css: { color: '#ff0' } }); | ||
small.appendChild(document.createTextNode('осталось ')); | small.appendChild(document.createTextNode('осталось ')); | ||
| Строка 595: | Строка 713: | ||
small.appendChild(timerSpan); | small.appendChild(timerSpan); | ||
small.appendChild(document.createTextNode(' секунд')); | small.appendChild(document.createTextNode(' секунд')); | ||
tooltip.appendChild(small); | tooltip.appendChild(small); | ||
document.body.appendChild(tooltip); | document.body.appendChild(tooltip); | ||
| Строка 607: | Строка 724: | ||
function cleanup() { | function cleanup() { | ||
if (!selectionActive) return; | |||
selectionActive = false; | selectionActive = false; | ||
| Строка 612: | Строка 730: | ||
document.removeEventListener('touchend', touchHandler); | document.removeEventListener('touchend', touchHandler); | ||
if (timer) { | if (timer) { clearInterval(timer); timer = null; } | ||
if (selectionTimer) { clearTimeout(selectionTimer); selectionTimer = null; } | |||
} | |||
if ( | var tip = document.getElementById('blame-tooltip'); | ||
if (tip && tip.parentNode) tip.parentNode.removeChild(tip); | |||
addLog('Режим выделения отключён', 'info'); | |||
} | } | ||
| Строка 640: | Строка 751: | ||
function handleSelection() { | function handleSelection() { | ||
if (!selectionActive) return; | if (!selectionActive) return; | ||
var selection = window.getSelection(); | var selection = window.getSelection(); | ||
var selectedText = selection.toString().trim(); | var selectedText = selection.toString().trim(); | ||
| Строка 663: | Строка 773: | ||
addLog('Время вышло', 'warning'); | addLog('Время вышло', 'warning'); | ||
cleanup(); | cleanup(); | ||
} | } | ||
}, 20000); | }, 20000); | ||
} | } | ||
// ===== ПОИСК АВТОРА ===== | |||
function findAuthor(text) { | function findAuthor(text) { | ||
var pageInfo = getPageInfo(); | var pageInfo = getPageInfo(); | ||
| Строка 680: | Строка 784: | ||
addLog('Начинаем поиск для страницы: ' + escapeForLog(title), 'info'); | addLog('Начинаем поиск для страницы: ' + escapeForLog(title), 'info'); | ||
var logText = 'Ищем текст: "' + text.substring(0, 100) + | var logText = 'Ищем текст: "' + text.substring(0, 100) + (text.length > 100 ? '..."' : '"'); | ||
addLog(logText, 'info'); | addLog(logText, 'info'); | ||
| Строка 706: | Строка 809: | ||
findTextAppearance(revisions, text, title, function(result) { | findTextAppearance(revisions, text, title, function(result) { | ||
hideLoading(loadingDiv); | hideLoading(loadingDiv); | ||
if (result) { | if (result) { | ||
addLog('Текст найден! Автор: ' + escapeForLog(result.rev.user), 'success'); | addLog('Текст найден! Автор: ' + escapeForLog(result.rev.user), 'success'); | ||
| Строка 751: | Строка 853: | ||
return; | return; | ||
} | } | ||
if (!data.query || !data.query.pages || !data.query.pages[0]) { | if (!data.query || !data.query.pages || !data.query.pages[0]) { | ||
addLog('Странный ответ от API', 'error'); | addLog('Странный ответ от API', 'error'); | ||
| Строка 759: | Строка 860: | ||
var page = data.query.pages[0]; | var page = data.query.pages[0]; | ||
if (page.missing) { | if (page.missing) { | ||
addLog('Страница не найдена в API', 'error'); | addLog('Страница не найдена в API', 'error'); | ||
| Строка 777: | Строка 877: | ||
}); | }); | ||
}); | }); | ||
totalFetched += page.revisions.length; | totalFetched += page.revisions.length; | ||
addLog('Загружено ревизий: ' + totalFetched + '/' + maxRevisions, 'info'); | addLog('Загружено ревизий: ' + totalFetched + '/' + maxRevisions, 'info'); | ||
| Строка 787: | Строка 886: | ||
if (data.continue && totalFetched < maxRevisions) { | if (data.continue && totalFetched < maxRevisions) { | ||
continueParam = { | continueParam = { rvcontinue: data.continue.rvcontinue }; | ||
fetchRevisions(); | fetchRevisions(); | ||
} else { | } else { | ||
| Строка 819: | Строка 916: | ||
} | } | ||
if (content.indexOf(text) === -1) | if (content.indexOf(text) === -1) continue; | ||
addLog('Текст найден в ревизии от ' + rev.timestamp + ' (id: ' + rev.revid + ')', 'info'); | addLog('Текст найден в ревизии от ' + rev.timestamp + ' (id: ' + rev.revid + ')', 'info'); | ||
if (i === 0) { | if (i === 0) { | ||
foundRev = rev; | foundRev = rev; | ||
addLog('Текст есть в самой первой ревизии страницы', 'info'); | addLog('Текст есть в самой первой ревизии страницы', 'info'); | ||
| Строка 834: | Строка 928: | ||
var previousContent = oldestFirst[i - 1].content || ''; | var previousContent = oldestFirst[i - 1].content || ''; | ||
if (previousContent.indexOf(text) === -1) { | if (previousContent.indexOf(text) === -1) { | ||
foundRev = rev; | foundRev = rev; | ||
addLog('Найдено первое появление! Ревизия: ' + rev.revid, 'success'); | addLog('Найдено первое появление! Ревизия: ' + rev.revid, 'success'); | ||
| Строка 870: | Строка 963: | ||
var safeUser = escapeHtml(rev.user); | var safeUser = escapeHtml(rev.user); | ||
var safeComment = escapeHtml(rev.comment); | var safeComment = escapeHtml(rev.comment); | ||
var dark = isDarkTheme(); | var dark = isDarkTheme(); | ||
var colors = dark ? { | var colors = dark ? { | ||
bg: '#3a3d42', | bg: '#3a3d42', fg: '#ffffff', border: '#72777d', | ||
accent: '#8fb3ff', subtle: '#54595d', btnText: '#1a1a1a' | |||
accent: '#8fb3ff', | |||
} : { | } : { | ||
bg: '#ffffff', | bg: '#ffffff', fg: '#202122', border: '#a2a9b1', | ||
accent: '#3366cc', subtle: '#eaecf0', btnText: '#ffffff' | |||
accent: '#3366cc', | |||
}; | }; | ||
| Строка 892: | Строка 976: | ||
id: 'blame-overlay', | id: 'blame-overlay', | ||
css: { | css: { | ||
position: 'fixed', | position: 'fixed', top: '0', left: '0', right: '0', bottom: '0', | ||
background: 'rgba(0,0,0,0.5)', zIndex: '9999' | |||
background: 'rgba(0,0,0,0.5)', | |||
} | } | ||
}); | }); | ||
| Строка 905: | Строка 984: | ||
id: 'blame-result-modal', | id: 'blame-result-modal', | ||
css: { | css: { | ||
position: 'fixed', | position: 'fixed', top: '50%', left: '50%', | ||
transform: 'translate(-50%, -50%)', | transform: 'translate(-50%, -50%)', | ||
background: colors.bg, | background: colors.bg, color: colors.fg, | ||
border: '2px solid ' + colors.border, | border: '2px solid ' + colors.border, | ||
borderRadius: '8px', | borderRadius: '8px', padding: '20px', | ||
zIndex: '10000', maxWidth: '500px', | |||
zIndex: '10000', | |||
boxShadow: '0 5px 20px rgba(0,0,0,0.3)', | boxShadow: '0 5px 20px rgba(0,0,0,0.3)', | ||
fontFamily: 'sans-serif' | fontFamily: 'sans-serif' | ||
| Строка 921: | Строка 995: | ||
}); | }); | ||
modal.appendChild(createSafeElement('h3', { | |||
text: 'Найден автор текста', | text: 'Найден автор текста', | ||
css: { | css: { marginTop: '0', color: colors.fg } | ||
})); | |||
}) | |||
var userP = createSafeElement('p'); | var userP = createSafeElement('p'); | ||
userP.appendChild(createSafeElement('strong', { text: 'Участник:' })); | |||
userP.appendChild(document.createTextNode(' ' + safeUser)); | userP.appendChild(document.createTextNode(' ' + safeUser)); | ||
modal.appendChild(userP); | modal.appendChild(userP); | ||
var dateP = createSafeElement('p'); | var dateP = createSafeElement('p'); | ||
dateP.appendChild(createSafeElement('strong', { text: 'Дата правки:' })); | |||
dateP.appendChild(document.createTextNode(' ' + formattedDate)); | dateP.appendChild(document.createTextNode(' ' + formattedDate)); | ||
modal.appendChild(dateP); | modal.appendChild(dateP); | ||
var commentP = createSafeElement('p'); | var commentP = createSafeElement('p'); | ||
commentP.appendChild(createSafeElement('strong', { text: 'Комментарий:' })); | |||
commentP.appendChild(document.createTextNode(' "' + safeComment + '"')); | commentP.appendChild(document.createTextNode(' "' + safeComment + '"')); | ||
modal.appendChild(commentP); | modal.appendChild(commentP); | ||
var versionP = createSafeElement('p'); | var versionP = createSafeElement('p'); | ||
versionP.appendChild(createSafeElement('strong', { text: 'Версия:' })); | |||
versionP.appendChild(document.createTextNode(' ')); | versionP.appendChild(document.createTextNode(' ')); | ||
var versionUrl = scriptPath + '/index.php?title=' + encodeURIComponent(title) + '&oldid=' + rev.revid; | var versionUrl = scriptPath + '/index.php?title=' + encodeURIComponent(title) + '&oldid=' + rev.revid; | ||
var versionLink = createSafeLink(versionUrl, 'посмотреть эту версию'); | var versionLink = createSafeLink(versionUrl, 'посмотреть эту версию'); | ||
| Строка 961: | Строка 1026: | ||
var diffP = createSafeElement('p'); | var diffP = createSafeElement('p'); | ||
diffP.appendChild(createSafeElement('strong', { text: 'Дифф:' })); | |||
diffP.appendChild(document.createTextNode(' ')); | diffP.appendChild(document.createTextNode(' ')); | ||
var diffUrl = scriptPath + '/index.php?title=' + encodeURIComponent(title) + '&diff=prev&oldid=' + rev.revid; | var diffUrl = scriptPath + '/index.php?title=' + encodeURIComponent(title) + '&diff=prev&oldid=' + rev.revid; | ||
var diffLink = createSafeLink(diffUrl, 'посмотреть изменения'); | var diffLink = createSafeLink(diffUrl, 'посмотреть изменения'); | ||
| Строка 972: | Строка 1035: | ||
modal.appendChild(diffP); | modal.appendChild(diffP); | ||
modal.appendChild(createSafeElement('hr', { | |||
css: { | css: { border: 'none', borderTop: '1px solid ' + colors.subtle } | ||
})); | |||
}) | |||
var closeButton = createSafeElement('button', { | var closeButton = createSafeElement('button', { | ||
| Строка 984: | Строка 1043: | ||
text: 'Закрыть', | text: 'Закрыть', | ||
css: { | css: { | ||
background: colors.accent, | background: colors.accent, color: colors.btnText, | ||
border: 'none', padding: '8px 16px', | |||
border: 'none', | borderRadius: '4px', cursor: 'pointer' | ||
borderRadius: '4px', | |||
} | } | ||
}); | }); | ||
| Строка 1000: | Строка 1056: | ||
var m = document.getElementById('blame-result-modal'); | var m = document.getElementById('blame-result-modal'); | ||
var o = document.getElementById('blame-overlay'); | var o = document.getElementById('blame-overlay'); | ||
if (m && m.parentNode) m.parentNode.removeChild(m); | |||
if (m && m.parentNode) | if (o && o.parentNode) o.parentNode.removeChild(o); | ||
if (o && o.parentNode) | |||
} | } | ||
| Строка 1023: | Строка 1074: | ||
text: message || 'Загрузка...', | text: message || 'Загрузка...', | ||
css: { | css: { | ||
position: 'fixed', | position: 'fixed', top: '20px', right: '20px', | ||
background: bg, color: fg, | |||
background: bg, | |||
border: '1px solid ' + border, | border: '1px solid ' + border, | ||
padding: '10px 20px', | padding: '10px 20px', borderRadius: '4px', | ||
zIndex: '10000', fontFamily: 'sans-serif', | |||
zIndex: '10000', | |||
boxShadow: '0 2px 10px rgba(0,0,0,0.1)' | boxShadow: '0 2px 10px rgba(0,0,0,0.1)' | ||
} | } | ||
}); | }); | ||
document.body.appendChild(div); | document.body.appendChild(div); | ||
return div; | return div; | ||
| Строка 1042: | Строка 1087: | ||
function hideLoading(div) { | function hideLoading(div) { | ||
if (div && div.parentNode) | if (div && div.parentNode) div.parentNode.removeChild(div); | ||
} | } | ||
// ===== ЗАПУСК ===== | |||
var pageInfo = getPageInfo(); | var pageInfo = getPageInfo(); | ||
if (pageInfo.isArticle) { | if (pageInfo.isArticle) { | ||
if (document.readyState === 'complete') { | if (document.readyState === 'complete') { | ||