Участник:Krassotkin/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: 20210827
history: 20210825-20210827
*/

"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() {
 // console.log("categorynewsfeed::main: I'm here.");
 newsFeedLocationElement = document.getElementById("newsFeedLocation");
 if(newsFeedLocationElement == null) {
  console.log("categorynewsfeed:main: newsFeedLocation element not found.");
  return;
 }
 // newsFeedLocationElement.innerHTML = "<hr /><hr />";
 let articleUrlPrefix = wikinewsArticleUrlPrefix;
 let apiBaseUrl = wikinewsApiEndpoint;
 let categoryTitle = "";
 let firstHeadingElement = document.getElementById("firstHeading");
 if(firstHeadingElement == null) {
  // console.log("categorynewsfeed:main: firstHeading element not found.");
  let section_0 = document.getElementById("section_0");
  if(section_0 == null) {
   // console.log("categorynewsfeed:main: section_0 element not found.");
   return;
  }
  articleUrlPrefix = wikinewsMobileArticleUrlPrefix;
  apiBaseUrl = wikinewsApiMobileEndpoint;
  categoryTitle = section_0.innerHTML;
 } else {
  categoryTitle = firstHeadingElement.innerHTML;
 }
 let categoryPosition = categoryTitle.indexOf("Категория:");
 if(categoryPosition != 0) {
  console.log("categorynewsfeed:main: This is not a category.");
  return;
 }
 let categoryName = categoryTitle.substring(10);
 // 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();