patternjavascriptMinor
Showing links to Contributor Breakdown on Stackoverflow Docs
Viewed 0 times
showingcontributorstackoverflowlinksbreakdowndocs
Problem
So Stackoverflow Documentation has added "Contributor Breakdown". For any given topic (and example) people can display who contributed in which way.
That page is only available if you know the link though. And it's not linked publicly in any way. Since I like these hidden pages, but I keep forgetting the correct URLs to use, I wrote a short userscript to insert these links where relevant:
``
let elem = docu
That page is only available if you know the link though. And it's not linked publicly in any way. Since I like these hidden pages, but I keep forgetting the correct URLs to use, I wrote a short userscript to insert these links where relevant:
``
// ==UserScript==
// @name Documentation contributor links
// @namespace http://github.com/Vogel612/mini-se-userscripts/documentation-contributor-breakdown
// @version 0.1
// @description Add links to the contributor breakdown for topics and examples on stackoverflow documentation.
// @author Vogel612
// @include /https?:\/\/stackoverflow\.com\/documentation\/.*
// @grant none
// @run-at document-end
// ==/UserScript==
(function() {
// these are in here for debugging purposes
document.addLinks = addLinks;
document.addContributorLink = addContributorLink;
})();
// because the page is modified on SE side after loading. 2 secs should be enough to wait
setTimeout(addLinks, 2000);
function addLinks() {
'use strict';
let topicLink = document.querySelectorAll("a.doc-topic-link")[0];
let topicMenuContainer = document.querySelector("div.docs-menu.topic-menu");
addContributorLink("topic", topicLink.href.split('/')[5], topicMenuContainer);
let exampleLinks = document.querySelectorAll(".example-link a.doc-example-link");
let exampleMenus = document.querySelectorAll(".example-link div.example-menu.docs-menu");
for (var i = 0; i < exampleLinks.length; i++) {
let currentLink = exampleLinks[i];
let currentMenuContainer = exampleMenus[i];
addContributorLink("example", currentLink.href.split('/')[7], currentMenuContainer);
}
}
function addContributorLink(section, linkId, menu) {
'use strict';
let href = /documentation/contributors/${section}/${linkId}`;let elem = docu
Solution
You're using
You seem to have thought about readability. You're declaring variables for just that purpose here:
So why oh why do I see magic numbers in your code?
Of those 5, arguably the first and latter are acceptable. The 3rd and 4th are the most magical of the lot. You explained the first in a comment, but I'd personally move it to a variable.
'use strict'; in all functions except the top one. Why not declare it at the top of your file?You seem to have thought about readability. You're declaring variables for just that purpose here:
let currentLink = exampleLinks[i];
let currentMenuContainer = exampleMenus[i];So why oh why do I see magic numbers in your code?
setTimeout(addLinks, 2000);
let topicLink = document.querySelectorAll("a.doc-topic-link")[0];
topicLink.href.split('/')[5]
currentLink.href.split('/')[7]
menu.insertBefore(elem, menu.children[0]);Of those 5, arguably the first and latter are acceptable. The 3rd and 4th are the most magical of the lot. You explained the first in a comment, but I'd personally move it to a variable.
Code Snippets
let currentLink = exampleLinks[i];
let currentMenuContainer = exampleMenus[i];setTimeout(addLinks, 2000);
let topicLink = document.querySelectorAll("a.doc-topic-link")[0];
topicLink.href.split('/')[5]
currentLink.href.split('/')[7]
menu.insertBefore(elem, menu.children[0]);Context
StackExchange Code Review Q#141244, answer score: 7
Revisions (0)
No revisions yet.