I've got the following code which is ran inside tampermonkey (It's a userscript), but there is a variable that is being declared, used, then overwritten by the next line. I'm looking for a way to shorten this code, or possibly remove the line if I can get the same results another way.
$(function() {
/***** RELATED TAGS *****/
var oldtags = main_content.find('div#related-tags, .module.js-gps-related-tags');
var vctags = genSBWidget('Related Tags', '<svg aria-hidden="true" class="svg-icon" width="18" height="18" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"></path></svg>', 'relatedtags');
oldtags.before(vctags).children('h4, a').remove().parent();
vctags = main_content.find('div#visualcrumbs-relatedtags'); //THIS LINE
vctags.prepend(oldtags.children()).children().wrapAll('<div class="js-watched-tag-list grid gs4 py4 fw-wrap">').each(function() {
$(this).toggleClass('dno js-hidden js-tag grid--cell').children().slice(1).remove();
});
});
genSBWidget simply generates some HTML, not sure if this is really relevant but I'll post the function anyways:
function genSBWidget(title, icon, innerid) {
var widget = '\
<div class="s-sidebarwidget" style="margin-bottom:19.500px;">\
<div class="s-sidebarwidget--header grid">\
<span class="grid--cell mr4">'+ icon +'</span>\
<span class="grid--cell fl1">' + title + '</span>\
</div>\
<div id="visualcrumbs-'+ innerid +'" class="s-sidebarwidget--content fd-column"></div>\
</div>\
';
return widget;
}
Side not: This script just takes "Related tags" in the right sidebar of stackoverflow and puts it into one of the new style boxes in the sidebar, and gives it the same styling as Watched Tags.
My actual userscript works on more than just related tags, it converts everything in the sidebar into this new style box, what I'm hoping to do from this question is learn some new methods of doing what I'm trying to do with related tags, so I can expand this accross my entire script.
Here is a minimal example of the script:
// ==UserScript==
// @name Related Tags Beautifier
// @namespace https://github.com/GrumpyCrouton/Userscripts
// @version 1.0
// @description Customizes StackExchange to your liking.
// @author GrumpyCrouton
// @match *://*.stackoverflow.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var $ = window.jQuery;
/***** GLOBAL SITE CHANGES *****/
var main_content = $('body div.container').find('div#content');
$(function() {
/***** RELATED TAGS *****/
var oldtags = main_content.find('div#related-tags, .module.js-gps-related-tags');
var vctags = genSBWidget('Related Tags', '<svg aria-hidden="true" class="svg-icon" width="18" height="18" viewBox="0 0 24 24"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-6h2v6zm0-8h-2V7h2v2z"></path></svg>', 'relatedtags');
oldtags.before(vctags).children('h4, a').remove().parent();
vctags = main_content.find('div#visualcrumbs-relatedtags');
vctags.prepend(oldtags.children()).children().wrapAll('<div class="js-watched-tag-list grid gs4 py4 fw-wrap">').each(function() {
$(this).toggleClass('dno js-hidden js-tag grid--cell').children().slice(1).remove();
});
});
function genSBWidget(title, icon, innerid) {
var widget = '\
<div class="s-sidebarwidget" style="margin-bottom:19.500px;">\
<div class="s-sidebarwidget--header grid">\
<span class="grid--cell mr4">'+ icon +'</span>\
<span class="grid--cell fl1">' + title + '</span>\
</div>\
<div id="visualcrumbs-'+ innerid +'" class="s-sidebarwidget--content fd-column"></div>\
</div>\
';
return widget;
}
})();