Skip to content

Instantly share code, notes, and snippets.

@haridsv
Last active August 29, 2015 14:14
Show Gist options
  • Save haridsv/8579abc195dcb077daa5 to your computer and use it in GitHub Desktop.
Save haridsv/8579abc195dcb077daa5 to your computer and use it in GitHub Desktop.
mcNewsSortOverlay.js: Show an overlay with a table of current news stories sorted by date on the MoneyControl portfolio news page.
javascript:s=document.createElement('script');s.type='text/javascript';document.body.appendChild(s);s.src='http://goo.gl/11CTWX';void(0);
if (typeof overlay == "undefined") {
var overlay = document.createElement("div");
overlay.id = "overlay";
overlay.setAttribute("style", "position: fixed; top: 30px; left: 30px; width: 800px; height: 600px; overflow-y: auto; background: white; top:50%; left:50%; margin-top: -300px; margin-left: -400px; z-index: 9999;");
var newsTable = document.createElement("table");
newsTable.id = "newsTable";
newsTable.setAttribute("style", "border: 1px solid black; border-collapse: collapse");
newsTable.width = "100%";
newsTable.height = "100%";
overlay.appendChild(newsTable);
var addCell = function(r) {
var c = r.insertCell(-1);
c.setAttribute("style", "border: 1px solid black; padding: 1px; font: 12px arial, sans-serif");
return c;
}
function compareRows(r1, r2){
if (r1[0] < r2[0]) return 1;
if (r1[0] > r2[0]) return -1;
return 0;
}
var it = document.evaluate('//table[contains(@style, "ADB6C6")]' , document, null, XPathResult.ANY_TYPE , null );
// Get to the news table.
var today = new Date();
var t = it.iterateNext();
var cel, newsLink, newsDate, partDate, compName, dateParts, day, month, year, rows = [];
if (t) {
t = t.getElementsByTagName("table")[0];
for (var i = 0, row; row = t.rows[i]; i++) {
// Both heading and news lines have 2 cells, out of which the first cell is useless and the 2nd cell is either the company name or the news link.
cel = row.cells[1];
if (!cel) break;
newsLink = cel.getElementsByTagName("a");
if (newsLink.length != 0) {
partDate = cel.getElementsByTagName("font")[0].textContent;
dateParts = partDate.split("-"); day = dateParts[0]; month = dateParts[1]; year = today.getFullYear();
if (month - 1 != today.getMonth() && month == 12) {
year = year - 1;
}
newsDate = new Date(year, month - 1, day);
newsLink = newsLink[0];
}
else {
compName = cel.textContent;
newsLink = null;
}
if (newsLink) {
rows.push([newsDate, compName, newsLink.cloneNode(true)]);
}
}
rows.sort(compareRows);
for (var i = 0; i < rows.length; i++) {
if (newsLink) {
row = newsTable.insertRow(-1);
addCell(row).innerHTML = rows[i][0].toISOString().slice(0, 10);
addCell(row).innerHTML = rows[i][1];
addCell(row).appendChild(rows[i][2]);
}
}
}
// When a link is clicked mark all the duplicates with yellow background.
overlay.onclick = function(e) {
var tgt = e.toElement;
if (tgt.tagName == "A") {
var nodesSnapshot = document.evaluate('//a[@href="'+tgt.href+'"]', overlay, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for ( var i=0 ; i < nodesSnapshot.snapshotLength; i++ ) {
nodesSnapshot.snapshotItem(i).setAttribute("style", "background: yellow;");
}
}
}
// Also disable all timers so that the page will not reload automatically.
// An ugly workaround from: http://stackoverflow.com/a/8345814/95750 that works in Chrome, but may not in other browsers.
var highestTimeoutId = setTimeout(";");
for (var i = 0 ; i < highestTimeoutId ; i++) {
clearTimeout(i);
}
}
document.body.appendChild(overlay);
overlay.tabIndex = 1; // Looks like this is needed to receive key events.
overlay.onkeyup = function(e){ if(e.keyCode == 27) document.body.removeChild(overlay); };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment