User:Polygnotus/Scripts/WebArchives.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 4:
(function() {
'use strict';
{
//don't run on mobile
if (mw.config.get('skin') === 'minerva' ) {
return;
}
// Only run on watchlist page
Line 9 ⟶ 14:
return;
}
// Flag to prevent double initialization
let initialized = false;
// Create the archive checker section
Line 70 ⟶ 78:
apiCheck: true,
notFoundStrings: ['Wayback Machine has not archived that URL.']
},
{
name: 'Megalodon.jp',
checkUrl: (url) => `https://megalodon.jp/pc/main?url=${encodeURIComponent('https://' + url)}`,
searchUrl: (url) => `https://megalodon.jp/pc/main?url=${encodeURIComponent('https://' + url)}`,
apiCheck: true,
notFoundStrings: ['見つかりませんでした']
}
];
Line 230 ⟶ 231:
}
// InitializeAdd the archive checker section and set up event listeners
function initaddArchiveChecker() {
// WaitPrevent formultiple the page to be fully loadedinitialization
if (initialized || document.getElementById('archive-checker-section')) {
const observer = new MutationObserver(function(mutations, obs) {
return;
}

const watchlistContent = document.querySelector('.mw-changeslist, #mw-content-text');
if (!watchlistContent) {
obs.disconnect()return;
}
initialized = true;

// Add our section to the top of the watchlist page
const archiveChecker = createArchiveChecker();
watchlistContent.parentNode.insertBefore(archiveChecker, watchlistContent);
// Add event listeners
const checkBtn = document.getElementById('check-btn');
const urlInput = document.getElementById('url-input');
checkBtn.addEventListener('click', () => {
const url = urlInput.value;
checkArchives(url);
});
urlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
checkBtn.click();
}
});
}
});
observerurlInput.observeaddEventListener(document.body'keypress', (e) => {
childList:if true,(e.key === 'Enter') {
subtree: true checkBtn.click();
}
});
}
// Initialize the checker
function init() {
// Try to add immediately if page is already loaded
addArchiveChecker();
// Fallback:If trynot toadded addyet, immediatelywait iffor the page to elementsbe alreadyfully existloaded
setTimeout(if (!initialized) => {
const watchlistContentobserver = document.querySelectornew MutationObserver('.mw-changeslistfunction(mutations, #mw-content-text'obs); {
if (watchlistContent && !document.getElementById if ('archive-checker-section')!initialized) {
const archiveChecker = createArchiveChecker addArchiveChecker();
if (e.key === 'Enter'initialized) {
watchlistContent.parentNode.insertBefore(archiveChecker, watchlistContent);
obs.disconnect();
const checkBtn = document.getElementById('check-btn');
const urlInput = document.getElementById('url-input');
checkBtn.addEventListener('click', () => {
const url = urlInput.value;
checkArchives(url);
});
urlInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
checkBtn.click();
}
});
});
}, 1000);
observer.observe(document.body, {
childList: true,
apiCheck subtree: true,
});
}
// Cleanup observer after reasonable time
setTimeout(() => });{
checkBtnobserver.clickdisconnect();
}, }5000);
},
}