User:Roc0ast3r/ArticleClass.js

// Make sure article is done loading (function($, mw) {   // Apply needed CSS    $("head").append(` .mw-page-title-class { margin-right: 5px; vertical-align: middle; }   .mw-page-title-class > img { height: 32px; vertical-align: middle; margin-bottom: 5px; }

.mw-page-title-namespace, .mw-page-title-separator, .mw-page-title-main { vertical-align: middle; } `)

const icons = { "FA": "b/bc/Featured_article_star.svg", "FL": "b/bc/Featured_article_star.svg", "FM": "b/bc/Featured_article_star.svg", "A": "2/25/Symbol_a_class.svg", "AL": "2/25/Symbol_a_class.svg", "GA": "9/94/Symbol_support_vote.svg", "B": "5/5f/Symbol_b_class.svg", "BL": "5/5f/Symbol_b_class.svg", "C": "e/e6/Symbol_c_class.svg", "CL": "e/e6/Symbol_c_class.svg", "Start": "a/a4/Symbol_start_class.svg", "Stub": "f/f5/Symbol_stub_class.svg", "List": "d/db/Symbol_list_class.svg", "Current": "2/2f/Symbol_current_class.svg", "Future": "6/6b/Symbol_future_class.svg", "SL": "a/a4/Symbol_start_class.svg", "SIA": "d/db/Symbol_list_class.svg", "Disambig": "2/2a/Symbol_dab_class.svg", "Redirect": "8/89/Symbol_redirect_vote2.svg", "Merge": "8/89/Symbol_redirect_vote2.svg", "Needed": "6/63/Symbol_needed_class.svg", }

// Start main code const pageName = mw.config.get("wgPageName") const namespace = mw.config.get("wgCanonicalNamespace") const categories = mw.config.get("wgCategories") let link = "/wiki/Wikipedia:Content_assessment" let articleClass = "e/e0/Symbol_question.svg"

let change = true // Put in some base class icons (these can be changed later) if (categories.includes("All stub articles")) articleClass = icons.Stub if (categories.includes("Disambiguation pages")) articleClass = "2/2a/Symbol_dab_class.svg" if (mw.config.get("wgIsRedirect") === true) articleClass = "8/89/Symbol_redirect_vote2.svg" if (document.getElementById("mw-indicator-featured-star")) articleClass = "e/e7/Cscr-featured.svg"

// These should not be changed if (mw.config.get("wgIsArticle") === false) { change = false articleClass = "6/6a/Symbol_na_class.svg" }   if (namespace === "Draft" || namespace === "Draft_talk") { change = false articleClass = "0/09/Symbol_draft_class.svg" }   if (namespace === "Category" || namespace === "Category_talk") { change = false articleClass = "9/96/Symbol_category_class.svg" }   if (namespace === "Template" || namespace === "Template_talk") { change = false articleClass = "5/5c/Symbol_template_class.svg" }   if (namespace === "File" || namespace === "File_talk") { change = false articleClass = "9/9c/Symbol_file_class.svg" }   if (namespace === "Portal" || namespace === "Portal_talk") { change = false articleClass = "e/e2/Symbol_portal_class.svg" }   if (namespace === "Project" || namespace === "Project_talk") { change = false articleClass = "7/72/Symbol_project_class.svg" }   if (namespace === "User" || namespace === "User_talk") { change = false articleClass = "1/13/Symbol_user_class.svg" }

// Get article assessment let talkPrefix = "Talk" const talkPage = `${talkPrefix}:${pageName.replace(/^(Talk|User_talk|Template_talk|Wikipedia_talk):/, "")}`

$.getJSON(`https://en.wikipedia.org/w/api.php?action=query&prop=categories&titles=${encodeURIComponent(talkPage)}&format=json&cllimit=500`, function(data) {       data = data.query.pages[Object.keys(data.query.pages)[0]]

if (data.hasOwnProperty("categories") && change === true) { data = data.categories

// Turn object of categories into array let categories = [] for (let category of data) { category = category.title.replace("Category:", "")

// Filters out categories that aren't class-related if (!category.match(/(.*?)-Class/i) || category.match(/(.*?)-importance/i)) continue

categories.push(category) }

let assessment = [ ["SIA", 0],     // 0 ["SL", 0],      // 1 ["List", 0],    // 2 ["Stub", 0],    // 3 ["Start", 0],   // 4 ["CL", 0],      // 5 ["C", 0],       // 6 ["BL", 0],      // 7 ["B", 0],       // 8 ["GA", 0],      // 9 ["AL", 0],      // 10 ["A", 0],       // 11 ["FL", 0],      // 12 ["FA", 0],      // 13 ["Future", 0],  // 14 ["Current", 0], // 15 ["Merge", 0],	 // 16 ["Redirect", 0], // 17 ["Needed", 0]   // 18 ["Disambig", 0] // 19 ]

// Iterage through the categories, and sort them for (let category of categories) { if (category.startsWith("SIA-Class")) assessment[0][1] = assessment[0][1] += 1 if (category.startsWith("SL-Class")) assessment[1][1] = assessment[1][1] += 1 if (category.startsWith("List-Class")) assessment[2][1] = assessment[2][1] += 1 if (category.startsWith("Stub-Class")) assessment[3][1] = assessment[3][1] += 1 if (category.startsWith("Start-Class")) assessment[4][1] = assessment[4][1] += 1 if (category.startsWith("CL-Class")) assessment[5][1] = assessment[5][1] += 1 if (category.startsWith("C-Class")) assessment[6][1] = assessment[6][1] += 1 if (category.startsWith("BL-Class")) assessment[7][1] = assessment[7][1] += 1 if (category.startsWith("B-Class")) assessment[8][1] = assessment[8][1] += 1 if (category.startsWith("GA-Class")) assessment[9][1] = assessment[9][1] += 1 if (category.startsWith("AL-Class")) assessment[10][1] = assessment[10][1] += 1 if (category.startsWith("A-Class")) assessment[11][1] = assessment[11][1] += 1 if (category.startsWith("FL-Class")) assessment[12][1] = assessment[12][1] += 1 if (category.startsWith("FA-Class")) assessment[13][1] = assessment[13][1] += 1 if (category.startsWith("Future-Class")) assessment[14][1] = assessment[14][1] += 1 if (category.startsWith("Current-Class")) assessment[15][1] = assessment[15][1] += 1 if (category.startsWith("Merge-Class")) assessment[16][1] = assessment[16][1] += 1 if (category.startsWith("Redirect-Class")) assessment[17][1] = assessment[17][1] += 1 if (category.startsWith("Needed-Class")) assessment[18][1] = assessment[18][1] += 1 if (category.startsWith("Disambig-Class")) assessment[19][1] = assessment[19][1] += 1 }

assessment.sort((a, b) => b[1] - a[1]) // Sort the array const finalAssessment = assessment[0][0] // Get the final assessment

// Get the icon and set it (and make sure it isn't 0) if (assessment[0][1] !== 0) articleClass = icons[finalAssessment]

// Change link depending on class if (finalAssessment === "FA") link = "/wiki/Wikipedia:Featured_articles" else if (finalAssessment === "FL") link = "/wiki/Wikipedia:Featured_lists" else if (finalAssessment === "FM") link = "/wiki/Wikipedia:Featured_pictures" else if (finalAssessment === "GA") link = "/wiki/Wikipedia:Good_articles" else if (finalAssessment === "Stub") link = "/wiki/Wikipedia:Stub"

// Add the icon to the title html } else { // Add the icon to the title html }   })

function html { $("#firstHeading").prepend(``) } }(jQuery, mediaWiki))