// ==UserScript== // @name SO - Code line nums // @namespace http://mdev.me/ // @Author SmokeyPHP // @contributor SmartManoj // @description Show line numbers for coding in question // @match *://*.stackoverflow.com/* // @match *://*.stackexchange.com/* // @match *://*.superuser.com/* // @match *://*.serverfault.com/* // @match *://*.askubuntu.com/* // @match *://*.stackapps.com/* // @match *://*.mathoverflow.net/* // @exclude *://data.stackexchange.com/* // @exclude *://api.stackexchange.com/* // @version 7 // @grant none // @updateURL https://raw.githubusercontent.com/SmartManoj/SmartUserScripts/master/SO_Lines.user.js // ==/UserScript== var styles = document.createElement('style'); styles.type = 'text/css'; styles.innerHTML = `#question pre { position: relative; } .mdevLinesToggle { top: 2px; right: 15px; width: 20px; color: #111 !important; height: 20px; cursor: pointer !important; font-size: 12px; background: #CCC; line-height: 20px; position: absolute; text-align: center; } .mdevLineBox { top: 11px; left: 5px; max-width: 40px; position: absolute; } .mdevLineNo { clear: both; color: #111; padding: 0 5px; display: block; background: #CCC; text-align: right; } pre.mdevLinesDisabled .mdevLinesToggle, pre.mdevLinesDisabled .mdevLineBox { display: none; }`; document.querySelector('head').appendChild(styles); var initLines = function() { var codeBlocks = N('body').findAll('#question pre, .answer pre, .wmd-preview pre'); O(codeBlocks).loop(function(n){ n = N(n); if(n.hasClass('mdevLinesEnabled') || n.hasClass('mdevLinesDisabled')) { n.removeClass('mdevLinesEnabled');n.removeClass('mdevLinesDisabled'); N(n.find('.mdevLineBox')).remove(); N(n.find('.mdevLinesToggle')).remove(); n.n.style.paddingLeft = ''; } n.n.style.position = 'relative'; var codeEl = n.find("code"); if(!codeEl) return; var codeH = codeEl.offsetHeight; var span = N(codeEl).find("span"); var spanH = 17; var lineNoOffset = 1; if(span) { spanH = span.offsetHeight; var spanLines = span.innerHTML.match(/\r?\n/g); spanLines = spanLines ? spanLines.length+1 : 1; spanH = spanH/spanLines; if(N(span).find('span')) span = N(span).find('span'); var m = null if(m = span.innerHTML.match(/^#Line:(\d+)/)) { lineNoOffset = D(m[1]).toInt(); } } else { spanH = parseInt(N(codeEl).getCompStyle('lineHeight')); if(m = codeEl.innerHTML.match(/^#Line:(\d+)/)) { lineNoOffset = D(m[1]).toInt(); } } var firstSpan = N(codeEl).find("span"); var lastSpan = N(codeEl).findAll("span"); lastSpan = lastSpan[lastSpan.length-1]; var lines = Math.floor(codeH/spanH); if(firstSpan.innerHTML.match(/^\s*\r?\n\s*$/)) lines++; if(lastSpan.innerHTML.match(/^\s*\r?\n\s*$/)) lines++; if(lines>5) { n.addClass('mdevLinesEnabled'); var lineBox = document.createElement('div'); lineBox.className = 'mdevLineBox'; lineBox.style.visibility = 'hidden'; var lineNosAdded = 0; while(lineNosAdded