Skip to content

Instantly share code, notes, and snippets.

@havremunken
Last active February 12, 2018 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save havremunken/f2692a113d5f8d5eb85fab4d17515700 to your computer and use it in GitHub Desktop.
Save havremunken/f2692a113d5f8d5eb85fab4d17515700 to your computer and use it in GitHub Desktop.
Et forsøk på litt fargehysteri
// ==UserScript==
// @name Fixrbkweb
// @version 1.0
// @description Legg til ukedager i rbkweb.no sin kampoversikt
// @author Sinna mann
// @match http://www.rbkweb.no/kamper*.php
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
(function() {
'use strict';
// Your code here...
var weekdays= ["Søn", "Man", "Tir", "Ons", "Tor", "Fre", "Lør"];
// Endre for å matche dine preferanser her
const colorResults = true,
colorNextMatch = true,
nextMatchColor = 'lightgreen',
winColor = '#AAFFAA',
drawColor = '#FFFFAA',
lossColor = '#FFAAAA';
// Sannsynligvis ikke behov for å tukle med noe under denne linja
// Tegn vi vil bytte ut for å slippe å introdusere kjipe linjeskift
var replacements = {
' ': ' ',
'-': '‑'
};
var year = document.URL.match(/rbkweb\.no\/kamper(\d\d\d\d)\.php/)[1];
var now = new Date();
var foundNext = false;
$("center > table > tbody > tr").has("td.kamp").each(function() {
var match = $(this).children("td:first-child").html().match(/(\d*)\/(\d*)/);
var myDateString = year + "-" + match[2] + "-" + match[1];
var date = new Date(myDateString);
var myday = date.getDay();
$(this).children("td:first-child").html(weekdays[myday] +" "+match[1]+"/"+match[2]);
var timeString = $(this).children("td").eq(3).text().match(/(\d\d):(\d\d)/);
if(colorNextMatch && timeString) {
var kampStart = new Date(myDateString + " " + timeString[1]+":"+timeString[2]);
var kampSlutt = new Date(kampStart);
kampSlutt.setMinutes(kampStart.getMinutes() + 105);
if(!foundNext && kampSlutt >= now) {
foundNext = true;
$(this).children("td").css('background-color', nextMatchColor);
}
} else if(colorResults) {
// Fargelegg resultater
var resultColor = "grey";
var homeMatch = $(this).children("td").eq(1).text().startsWith("Rosenborg");
var goals = $(this).children("td").eq(3).text().match(/(\d+)-(\d*)/);
if(goals) {
if(goals[1]==goals[2])
resultColor = drawColor;
if(goals[1]>goals[2]) {
resultColor = homeMatch ? winColor : lossColor;
}
if(goals[1]<goals[2]) {
resultColor = homeMatch ? lossColor : winColor;
}
$(this).children("td").eq(3).css('background-color', resultColor);
}
}
// Fiks så ingen tabell-celler går over mer enn én linje.
$(this).children("td").each(function(){
var element = $(this);
if(element.children("a").length > 0)
element = element.children("a");
var content = element.html();
for(var rep in replacements) {
content = content.replace(new RegExp(rep, "g"), replacements[rep]);
}
element.html(content);
});
});
})();
@larsjaas
Copy link

larsjaas commented Feb 9, 2018

console.log("test"), and the weekdays array setup...

@havremunken
Copy link
Author

Oops, done!

@larsjaas
Copy link

larsjaas commented Feb 9, 2018

Avoid breaks in the match column like this:

var myday = date.getDay();
docs[i].innerHTML = weekdays[myday] +" "+ mynode;
$(docs[i]).siblings().eq(0).html($(docs[i]).siblings().eq(0).html().replace(/ /g, " "));
$(docs[i]).siblings().eq(0).html($(docs[i]).siblings().eq(0).html().replace(/-/g, "‑"));

The last dash is "&# 8209;" without the space.

@havremunken
Copy link
Author

Did your replacing lines on eq(1) as well (the Kamptype column) and finally all table rows seem to be content with only being one single line... at least on my machine, in Chrome etc. :)

@havremunken
Copy link
Author

...and did it to the TV column as well, had some issues on the 2017 page at least.

@havremunken
Copy link
Author

Ok, there is something going wrong here, seeing how we're replacing with html() more stuff than intended is getting overwritten. Destroying links in the TV column. Will have another look once the weekend brain kicks in.

@havremunken
Copy link
Author

jQueryfied quite a bit more, now doing updates for all table cells in a loop, and using object properties to define the replacement characters. Such clever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment