Last active
August 29, 2015 14:14
-
-
Save LordJZ/630eec63085af03e8a8b to your computer and use it in GitHub Desktop.
Some fixes for BazQux Reader. Mainly reddit inline support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name BQ Fixes | |
| // @namespace bazqux | |
| // @include https://bazqux.com/* | |
| // @version 1 | |
| // ==/UserScript== | |
| function createButton(text, onclick) { | |
| $('<a/>') | |
| .attr('class', 'button') | |
| .append($('<span/>').attr('class', 'buttonText').text(text)) | |
| .on('click', onclick) | |
| .appendTo($('.subToolBar')); | |
| } | |
| let imgurRegex = /^https?:\/\/(?:www\.)?(?:[im]\.)?imgur\.com\/([\w\d]+)(?:\.([\w\d]+))?\/?(?:\?.*)?$/; | |
| let imguraRegex = /^https?:\/\/(?:www\.|m\.)?imgur\.com\/a\/([\w\d]+)(?:\/embed)?\/?(?:\?.*)?$/; | |
| let tumblrRegex = /^https?:\/\/(?:\d+\.)?media\.tumblr\.com\//; | |
| let youtubeRegex = /^(?:https?:\/\/)?(?:www\.)?youtu(?:be\.com\/watch\?(?:.*?&?(?:amp;)?)?v=|\.be\/)([\w\-]+)(?:&?(?:amp;)?[\w\?=]*)?/; | |
| function extractFullUrl(url) { | |
| if (!url) return; | |
| let m; | |
| if (m = url.match(imgurRegex)) { | |
| var ext = m[2]; | |
| var prefix = ''; | |
| switch (ext) { | |
| case 'webm': | |
| case 'gif': | |
| ext = 'gifv'; // fucking noscript | |
| case 'gifv': | |
| prefix = '+'; | |
| break; | |
| } | |
| return prefix + 'http://i.imgur.com/' + m[1] + (ext ? '.' + ext : '.jpg'); | |
| } | |
| if (m = url.match(imguraRegex)) { | |
| return '!http://imgur.com/a/' + m[1] + '/embed'; | |
| } | |
| if (m = url.match(youtubeRegex)) { | |
| return '+http://www.youtube.com/embed/' + m[1] + '?autoplay=0&fs=1'; | |
| } | |
| if (tumblrRegex.test(url)) { | |
| return url; | |
| } | |
| } | |
| function processPostFrame(msgFrame) { | |
| let $e = $(msgFrame); | |
| let redditThumb = $e.find('img.bqrRedditThumb')[0]; | |
| if (redditThumb) { | |
| let a = redditThumb.parentNode; | |
| if (a.tagName == 'A') { | |
| let url = extractFullUrl(a.href); | |
| if (!url) return; | |
| let $mtext = $e.find('.mtext').empty(); | |
| if (url[0] == '!') { | |
| $mtext.append( | |
| $('<iframe/>') | |
| .attr('src', url.substr(1)) | |
| .css({height: '85vh', width: '100%'}) | |
| ); | |
| } else if (url[0] == '+') { | |
| $mtext.append( | |
| $('<iframe/>') | |
| .attr('src', url.substr(1)) | |
| .attr('class', 'bqrMsgVideo') | |
| ); | |
| } else { | |
| $mtext.append($('<img/>').attr('src', url)); | |
| } | |
| } | |
| } | |
| } | |
| setTimeout(function() { | |
| var shown = true; | |
| createButton('RT', function() { | |
| var regex2 = /\(.+?\)|\[.+?\]/g; | |
| var regex = /^(?:(?:\(.+?\)|\[.+?\])\s*)+/; | |
| for (var items = $('.msubject'), l = items.length, i = 0; | |
| i < l; | |
| ++i) { | |
| var item = $(items[i]); | |
| var html = item.html(); | |
| var before; | |
| html = html.replace(regex, function() { | |
| before = arguments[0]; | |
| return ''; | |
| }); | |
| if (before) { | |
| html += ' – ' + before; | |
| } | |
| html = html.replace(regex2, function () { | |
| return '<span style="color: lightgray;">' + arguments[0] + '</span>'; | |
| }); | |
| item.html(html); | |
| } | |
| }); | |
| let obs = new MutationObserver(function(mutations, observer) { | |
| // look through all mutations that just occured | |
| for(let i=0; i<mutations.length; ++i) { | |
| let added = mutations[i].addedNodes; | |
| for(let j=0; j<added.length; ++j) { | |
| let e = added[j]; | |
| if (!e.classList.contains('msgFrame')) { | |
| continue; | |
| } | |
| processPostFrame(e); | |
| } | |
| } | |
| }); | |
| obs.observe( document.getElementsByClassName('innerMsgs')[0], { | |
| childList: true, | |
| subtree: true | |
| }); | |
| $('.msgFrame').each(function (i, e) { processPostFrame(e); }) | |
| /*$('body').css('right', '500px').append( | |
| $('iframe').attr('src', 'https://web.whatsapp.com/').css({ | |
| 'position': 'absolute', | |
| 'width': '500px', | |
| 'right': '-500px', | |
| 'top': '0', | |
| 'bottom': '0', | |
| 'height': '100%' | |
| }) | |
| );*/ | |
| }, 5000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment