Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name SEImageExpander
- // @namespace http://vulpin.com/
- // @description Let people click on images to view the full size
- // @include http*://*.stackexchange.com/*
- // @include http*://*.superuser.com/*
- // @include http*://superuser.com/*
- // @include http*://*.serverfault.com/*
- // @include http*://serverfault.com/*
- // @include http*://*.stackoverflow.com/*
- // @include http*://stackoverflow.com/*
- // @version 1.0.1
- // @grant none
- // ==/UserScript==
- (function() {
- var Program = {
- main: function() {
- window.removeEventListener('load', mainEventListener, false);
- var observer = new MutationObserver(function(mutations, observer) {
- // y'know what? MutationObserver sucks.
- // it doesn't give every modified node, just the top of a tree of modified nodes
- mutations.forEach(function(mutation) {
- Array.prototype.forEach.call(mutation.addedNodes, Program.processImgDescendants);
- });
- });
- observer.observe(document, {
- subtree: true,
- childList: true
- });
- Program.processImgDescendants(document);
- },
- processImgDescendants: function(node) {
- var imgNodes = node.querySelectorAll('.post-text img');
- Array.prototype.forEach.call(imgNodes, function(imgNode) {
- Program.processNode(imgNode);
- });
- },
- processNode: function(node) {
- if (node.tagName.toUpperCase() !== 'IMG') {
- return;
- }
- // walk up the tree until we reach the main post div, we don't care what's beyond that
- var parent = node;
- while (!parent.classList.contains('post-text')) {
- // if this image is already within a link, don't touch it
- if (parent.tagName.toUpperCase() === 'A') {
- return;
- }
- parent = parent.parentNode;
- // have we made it all the way to the top? we're not in a post!
- if (parent === null) {
- return;
- }
- }
- // ok, now we know the node is an unlinked image within a post
- var link = document.createElement('a');
- link.href = node.src;
- link.target = '_blank';
- node.parentNode.replaceChild(link, node);
- link.appendChild(node);
- }
- };
- var mainEventListener = Program.main.bind(Program);
- window.addEventListener('load', mainEventListener, false);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement