İstifadəçi:Qədir/FindCategoriesDemo.js
(İstifadəçi:Gadir/FindCategoriesDemo.js səhifəsindən yönləndirilmişdir)
(function() {
// Stil əlavə et
const style = document.createElement('style');
style.textContent = `
#catModalOverlay {
position: fixed; top:0; left:0; width: 100%; height:100%;
background: rgba(0,0,0,0.5);
z-index: 9998;
display: none;
}
#catModal {
position: fixed; top: 50%; left: 50%;
transform: translate(-50%, -50%);
background: white; padding: 20px;
max-width: 500px; max-height: 80vh; overflow-y: auto;
box-shadow: 0 0 10px black;
z-index: 9999;
display: none;
font-family: Arial, sans-serif;
border-radius: 6px;
}
#catModalClose {
position: absolute; top: 8px; right: 10px;
cursor: pointer; font-weight: bold;
background: none; border: none; font-size: 20px;
}
#catList {
max-height: 300px; overflow-y: auto;
border-top: 1px solid #ccc; margin-top: 10px; padding-top: 10px;
}
#catModal button {
margin-top: 10px;
}
#interwikiLinks a {
margin-right: 10px;
font-weight: bold;
}
#categoryFinder {
font-size: small;
display: block;
margin-top: 5px;
cursor: pointer;
color: #0645ad;
text-decoration: underline;
}
`;
document.head.appendChild(style);
// HTML elementləri əlavə et
const finderLink = document.createElement('a');
finderLink.id = 'categoryFinder';
finderLink.textContent = '(kateqoriya tap)';
finderLink.href = '#';
document.querySelector('#firstHeading')?.after(finderLink);
const overlay = document.createElement('div');
overlay.id = 'catModalOverlay';
document.body.appendChild(overlay);
const modal = document.createElement('div');
modal.id = 'catModal';
modal.innerHTML = `
<button id="catModalClose">X</button>
<h3>Kateqoriya tap</h3>
<div><b>Interviki (azwiki çıxarıldı):</b> <span id="interwikiLinks"></span></div>
<div style="margin-top:10px;">
<input id="langInput" placeholder="Dil kodu (məs: en)" style="width:70px" maxlength="10"/>
<input id="pageInput" placeholder="Səhifə adı (məs: Apple)" style="width:calc(100% - 80px)" />
<button id="searchBtn">Axtar</button>
</div>
<div id="message" style="font-style: italic; margin-top:10px;"></div>
<div id="catList"></div>
`;
document.body.appendChild(modal);
// Elementlərə referans
const closeBtn = modal.querySelector('#catModalClose');
const langInput = modal.querySelector('#langInput');
const pageInput = modal.querySelector('#pageInput');
const searchBtn = modal.querySelector('#searchBtn');
const message = modal.querySelector('#message');
const catList = modal.querySelector('#catList');
const interwikiLinks = modal.querySelector('#interwikiLinks');
// Modal açıb-bağlama funksiyaları
function showModal() {
overlay.style.display = 'block';
modal.style.display = 'block';
langInput.focus();
}
function hideModal() {
overlay.style.display = 'none';
modal.style.display = 'none';
message.textContent = '';
catList.innerHTML = '';
langInput.value = '';
pageInput.value = '';
interwikiLinks.innerHTML = '';
}
// Mövcud səhifənin wikidata elementi və interviki yüklə
async function loadInterwikis() {
message.textContent = 'Yüklənir...';
interwikiLinks.textContent = '';
const pageTitle = mw.config.get('wgPageName');
const url = `https://az.wikipedia.org/w/api.php?action=query&prop=pageprops&titles=${encodeURIComponent(pageTitle)}&format=json&origin=*`;
try {
const data = await fetch(url).then(r => r.json());
const pages = data.query.pages;
const props = Object.values(pages)[0].pageprops;
if (!props || !props.wikibase_item) {
message.textContent = 'Vikidata elementi tapılmadı.';
return null;
}
const entityId = props.wikibase_item;
const wdUrl = `https://www.wikidata.org/wiki/Special:EntityData/${entityId}.json`;
const wdData = await fetch(wdUrl).then(r => r.json());
const sitelinks = wdData.entities[entityId].sitelinks;
if (!sitelinks) {
message.textContent = 'Interviki tapılmadı.';
return null;
}
const filtered = Object.entries(sitelinks).filter(([k]) => k !== 'azwiki');
if (filtered.length === 0) {
message.textContent = 'Interviki yoxdur.';
return null;
}
filtered.forEach(([key,val]) => {
const lang = key.replace('wiki','');
const page = val.replace(/_/g,' ');
const a = document.createElement('a');
a.href = `https://${lang}.wikipedia.org/wiki/${val}`;
a.target = '_blank';
a.textContent = `${lang} (${page})`;
interwikiLinks.appendChild(a);
});
message.textContent = '';
return entityId;
} catch(e) {
message.textContent = 'Xəta baş verdi.';
console.error(e);
return null;
}
}
// Kateqoriyalar yüklə və seçmək üçün göstər
async function loadCategories(langPage) {
message.textContent = 'Kateqoriyalar yüklənir...';
catList.innerHTML = '';
const [lang, ...pageParts] = langPage.split(':');
const page = pageParts.join(':').trim().replace(/ /g, '_');
try {
const apiUrl = `https://${lang}.wikipedia.org/w/api.php?action=query&prop=pageprops&titles=${encodeURIComponent(page)}&format=json&origin=*`;
const data = await fetch(apiUrl).then(r => r.json());
const pages = data.query.pages;
const entityId = Object.values(pages)[0].pageprops?.wikibase_item;
if (!entityId) {
message.textContent = 'Vikidata elementi tapılmadı.';
return;
}
const wdUrl = `https://www.wikidata.org/wiki/Special:EntityData/${entityId}.json`;
const wdJson = await fetch(wdUrl).then(r => r.json());
const claims = wdJson.entities[entityId].claims.P910 || [];
if (claims.length === 0) {
message.textContent = 'Heç bir kateqoriya tapılmadı.';
return;
}
const currentCatsUrl = `https://az.wikipedia.org/w/api.php?action=query&prop=categories&titles=${encodeURIComponent(mw.config.get('wgPageName'))}&cllimit=max&format=json&origin=*`;
const currentCatsData = await fetch(currentCatsUrl).then(r => r.json());
const pagesData = currentCatsData.query.pages;
const currentCats = Object.values(pagesData)[0].categories?.map(c => c.title) || [];
let html = `
<button id="selectAllBtn">Hamısını seç</button>
<button id="deselectAllBtn">Heç birini seçmə</button><br><br>
`;
claims.forEach(claim => {
const catId = claim.mainsnak.datavalue.value.id;
const labelAz = wdJson.entities[catId]?.labels?.az?.value || '';
const labelEn = wdJson.entities[catId]?.labels?.en?.value || '';
const catTitle = labelAz || labelEn || catId;
if (currentCats.includes('Kateqoriya:' + catTitle)) return;
html += `<label><input type="checkbox" class="catCheckbox" checked data-title="${catTitle}"> ${catTitle}</label><br>`;
});
if (!html.includes('catCheckbox')) {
message.textContent = 'Əlavə ediləcək yeni kateqoriya yoxdur.';
return;
}
html += `<br><button id="addSelectedBtn">Əlavə et</button>`;
catList.innerHTML = html;
message.textContent = '';
document.getElementById('selectAllBtn').onclick = () => {
catList.querySelectorAll('.catCheckbox').forEach(c => c.checked = true);
updateAddBtn();
};
document.getElementById('deselectAllBtn').onclick = () => {
catList.querySelectorAll('.catCheckbox').forEach(c => c.checked = false);
updateAddBtn();
};
const addBtn = document.getElementById('addSelectedBtn');
addBtn.onclick = () => {
const selected = [];
catList.querySelectorAll('.catCheckbox').forEach(c => {
if (c.checked) selected.push(`[[Kateqoriya:${c.dataset.title}]]`);
});
if(selected.length === 0) {
alert('Heç bir kateqoriya seçilməyib.');
return;
}
insertCategories(selected.join('\n'));
message.textContent = 'Kateqoriyalar əlavə olundu.';
catList.innerHTML = '';
};
catList.querySelectorAll('.catCheckbox').forEach(chk => {
chk.onchange = updateAddBtn;
});
function updateAddBtn() {
const total = catList.querySelectorAll('.catCheckbox').length;
const checkedCount = [...catList.querySelectorAll('.catCheckbox')].filter(c=>c.checked).length;
if (checkedCount === 0) {
addBtn.textContent = 'Əlavə et';
addBtn.disabled = true;
} else if (checkedCount === total) {
addBtn.textContent = 'Əlavə et';
addBtn.disabled = false;
} else {
addBtn.textContent = 'Seçilənləri əlavə et';
addBtn.disabled = false;
}
}
updateAddBtn();
} catch(e) {
message.textContent = 'Xəta baş verdi.';
console.error(e);
}
}
function insertCategories(text) {
const textarea = document.getElementById('wpTextbox1');
if (!textarea) {
alert(' sahəsi tapılmadı!');
return;
}
textarea.value += '\n' + text;
}
// Eventlər
finderLink.addEventListener('click', async e => {
e.preventDefault();
showModal();
await loadInterwikis();
});
closeBtn.addEventListener('click', hideModal);
overlay.addEventListener('click', hideModal);
searchBtn.addEventListener('click', () => {
const lang = langInput.value.trim();
const page = pageInput.value.trim();
if (!lang || !page) {
alert('Dil kodu və səhifə adı lazımdır!');
return;
}
loadCategories(`${lang}:${page}`);
});
[langInput, pageInput].forEach(inp => {
inp.addEventListener('keydown', e => {
if(e.key === 'Enter') {
e.preventDefault();
searchBtn.click();
}
});
});
})();