MediaWiki:Gadget-categorynewsfeed.js
Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.
- Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
- Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
- Internet Explorer / Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
- Opera: Нажмите Ctrl+F5.
/*
categorynewsfeed.js creates a newsfeed in the categories of Russian Wikinews.
creator: Alexander Krassotkin ([[User:Krassotkin]]), Simon Krassotkin ([[User:DonSimon]])
created: 20210825
version: 20220903
history: 20210825-20220903
*/
"use strict";
const countOfNewsString = 50;
const wikinewsApiEndpoint = "https://ru.wikinews.org/w/api.php";
const wikinewsApiMobileEndpoint = "https://ru.m.wikinews.org/w/api.php";
const wikinewsArticleUrlPrefix = "https://ru.wikinews.org/wiki/";
const wikinewsMobileArticleUrlPrefix = "https://ru.m.wikinews.org/wiki/";
const wikinewsSearchStaticParams = "action=query&list=search&srnamespace=0&srsort=create_timestamp_desc&format=json";
let newsFeedLocationElement = null;
function main() {
newsFeedLocationElement = document.getElementById("newsFeedLocation");
if(newsFeedLocationElement == null) {
//console.log("categorynewsfeed:main: newsFeedLocation element not found.");
return;
}
// newsFeedLocationElement.innerHTML = "<hr /><hr />";
let articleUrlPrefix = "";
let apiBaseUrl = "";
var myUrl = document.location.href;
if(myUrl.indexOf("ru.m") >= 0) {
articleUrlPrefix = wikinewsMobileArticleUrlPrefix;
apiBaseUrl = wikinewsApiMobileEndpoint;
var back = document.getElementById("background");
if(back != null) {
document.getElementById("background").style.display = 'none';
}
} else {
articleUrlPrefix = wikinewsArticleUrlPrefix;
apiBaseUrl = wikinewsApiEndpoint;
}
let categoryTitle = "";
var categoryName = "";
var value = document.getElementById('firstHeading').innerText;
if(value == null) {
//console.log("categorynewsfeed:main: firstHeading element not found.");
var wiki_title = document.title;
value = wiki_title.slice(0, -14);
return;
}
var categoryState = value.substring(0, 10);
categoryTitle = value.substring(10);
if(categoryState == "Категория:") {
categoryName = categoryTitle;
} else {
console.log("categorynewsfeed:main: This is not a category.");
return;
}
// console.log("categorynewsfeed:main: This is a category by name: " + categoryName);
let encodedSearchString = encodeURI("incategory:\"" + categoryName + "\"");
encodedSearchString += "+" + encodeURI("-incategory:\"Архивные новости\"") + "+" + encodeURI("-incategory:\"Не публиковать\"");
encodedSearchString += "+" + encodeURI("-incategory:\"Ожидаемые события по датам\"") + "+" + encodeURI("-incategory:\"Викиновости коротко\"");
let wikinewsSearchUrl = apiBaseUrl + "?" + wikinewsSearchStaticParams + "&srlimit=" + countOfNewsString + "&srsearch=" + encodedSearchString;
// console.log("categorynewsfeed:main:wikinewsSearchUrl: " + wikinewsSearchUrl);
fetch(wikinewsSearchUrl).then(response => response.json()).then(data => processWikinewsList(data, articleUrlPrefix));
}
async function processWikinewsList(responseJson, articleUrlPrefix) {
// console.log("categorynewsfeed:processWikinewsList:responseJson: " + responseJson);
// console.log("categorynewsfeed:processWikinewsList:responseJson string: " + JSON.stringify(responseJson));
let countOfNews = responseJson.query.searchinfo.totalhits;
// console.log("categorynewsfeed:processWikinewsList:countOfNews: " + countOfNews);
let feeds = "";
if(countOfNews > 0) {
feeds = "<div style=\"border-bottom:1px solid #a2a9b1;font-family:'Linux Libertine','Georgia','Times',serif;font-size:1.5em;font-weight:normal;margin-top:1em;\">Последние новости</div>\n";
feeds += "<ul class=\"feeds\" style=\"font-size:1em;\">\n";
for(const articlejson of responseJson.query.search) {
let feedElement = " <li class=\"feedElement\"><a href=\"" + articleUrlPrefix + encodeURI(articlejson.title) + "\">" + articlejson.title + "</a></li>\n";
feeds += feedElement;
}
feeds += "</ul>";
}
newsFeedLocationElement.innerHTML = feeds;
}
main();