Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
On travel.stackexchange.com put pale red backgrounds on all tags which lack wiki excerpt
// ==UserScript==
// @name Hippietrail's travel.SE enhancements
// @description highlight tags without tag wiki excerpts
// @version 0.6
// @namespace hippietrail
// @match http://travel.stackexchange.com/*
// @match http://meta.travel.stackexchange.com/*
// ==/UserScript==
//
// .post-tag
//
// questions overview page
// favourite tags in sidebar
// individual question pages
// individual user page
// tags overview page
// ... just about everywhere ...
//
// .user-tags a
//
// users overview page
//
// same on meta
//
//
// http://userscripts.org/scripts/review/123588
//
function main($) {
'use strict';
jQuery.noConflict();
// get all tag elements and all tag names
var mainOrMeta = $('.meta-title').length ? 'meta.travel' : 'travel',
whichPage = $('body')[0].className,
whichSelector = whichPage === 'users-page' ? '.user-tags a' : '.post-tag',
$eleArray = $(whichSelector),
tagArray = $eleArray.map(function(i,e) { return e.innerText; });
// we only want each tag name once
tagArray = $.grep(tagArray, function(v, k){
return $.inArray(v ,tagArray) === k;
});
//console.log(tagArray.length + ' tags in total');
// we can only send 20 tags to the API at a time
var g_pageSize = 20;
var arrayOfSets = [];
while (tagArray.length) {
arrayOfSets.push(tagArray.splice(0, g_pageSize));
}
// we spliced everything out of it so we can't use it again anyway
delete window.tagArray;
//console.log(arrayOfSets.length + ' sets of ' + g_pageSize + ' tags');
// loop
function getWikis(tagSetNumber) {
var ourTags = arrayOfSets[tagSetNumber];
//console.log(tagSetNumber, ourTags);
var wikisParams = {
site: mainOrMeta,
filter: '!n6Tm_aay8D'
};
var wikisUrl = 'https://api.stackexchange.com/2.1/tags/' + ourTags.join(';') + '/wikis';
var wikisPromise = $.ajax({
url: wikisUrl + ($.support.cors ? '' : '?callback=?'),
data: $.param(wikisParams),
dataType: 'json',
success: function(data, textStatus, jqXHR) {
//console.log('wikis success: ', [data, textStatus, jqXHR]);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('wikis error: ', [textStatus, errorThrown]);
}
});
wikisPromise.pipe(function(data, textStatus, jqXHR) {
//console.log('pipe');
//console.log('total: ', data.total);
//console.log('items: ', data.items.length);
var hash = {};
$.each(data.items, function(i, e) {
hash[e.tag_name] = true;
});
//console.log(Object.keys(hash));
$.each(ourTags, function(i, e) {
$(whichSelector).filter(function() {
return $(this).text() === e;
}).css('background-color', e in hash ? '#E4F9D3': '#FD6D6F');
});
if (++tagSetNumber < arrayOfSets.length) {
getWikis(tagSetNumber);
}
});
}
getWikis(0);
}
function thirdParty($) {
'use strict';
jQuery.noConflict();
// Put third-party non-jQuery functions here. They'll be wrapped into the
// jQuery prototype in a moment.
//var sayHello = function (who) {
// alert('Hello ' + who + '!');
//}
jQuery.extend({
// If you have any non-jQuery functions, they need to be wrapped in here.
//sayHellow: function(who) {
// return sayHello('World');
//}
});
// Put third-party jQuery plugins, extensions, etc. here
}
!function loader(i) {
var script
, requires = [ 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js'
//, 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js'
]
, head = document.getElementsByTagName('head')[0]
, makeScript = function () {
script = document.createElement('script');
script.type = 'text/javascript';
}
, loadLocal = function (fn) {
makeScript();
script.textContent = '(' + fn.toString() + ')(jQuery);';
head.appendChild(script);
}
;
(function (i) {
makeScript();
script.src = requires[i];
script.addEventListener('load', function () {
++i !== requires.length ? loader(i) : (loadLocal(thirdParty), loadLocal(main));
}, true);
head.appendChild(script);
})(i || 0);
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment