Skip to content

Commit

Permalink
Attempt to preserve the original urls when following links.
Browse files Browse the repository at this point in the history
I created a system for preserving the original link (when necessary)
without the trouble of loading entire webpages to mark history.

When necessary, the original link is saved to a property called
hrefOverride. If that is detected during expand creation then
mouseover/mouseout handlers will be attached to swap the href on demand.

This works well but it can cause the link to turn blue on mouseover.
Without marking the history this has been made nearly impossible to
prevent in the name of user privacy.
  • Loading branch information
gamefreak committed May 2, 2012
1 parent 00f8d85 commit e5d7b75
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions lib/reddit_enhancement_suite.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4557,7 +4557,7 @@ modules['keyboardNav'] = {
if ((modules['keyboardNav'].options.commentsLinkNewTab.value) || e.ctrlKey) {
button = 1;
}
var thisURL = this.getAttribute('href');
var thisURL = this.hrefOverride || this.href;
if ((thisURL.indexOf('#') == -1) && (button == 1)) {
if (typeof(chrome) != 'undefined') {
var thisJSON = {
Expand Down Expand Up @@ -4591,10 +4591,10 @@ modules['keyboardNav'] = {
}
self.postMessage(thisJSON);
} else {
window.open(this.getAttribute('href'));
window.open(thisURL);
}
} else {
location.href = this.getAttribute('href');
location.href = thisURL;
}
}, true);
}
Expand Down Expand Up @@ -5135,7 +5135,7 @@ modules['keyboardNav'] = {
},
followLink: function(newWindow) {
var thisA = this.keyboardLinks[this.activeIndex].querySelector('a.title');
var thisHREF = thisA.getAttribute('href');
var thisHREF = thisA.hrefOverride || thisA.href;
// console.log(thisA);
if (newWindow) {
var button = (this.options.followLinkNewTabFocus.value) ? 0 : 1;
Expand Down Expand Up @@ -5179,7 +5179,7 @@ modules['keyboardNav'] = {
},
followComments: function(newWindow) {
var thisA = this.keyboardLinks[this.activeIndex].querySelector('a.comments');
var thisHREF = thisA.getAttribute('href');
var thisHREF = thisA.hrefOverride || thisA.href;
if (newWindow) {
if (typeof(chrome) != 'undefined') {
var thisJSON = {
Expand Down Expand Up @@ -7082,7 +7082,9 @@ modules['singleClick'] = {
var lcMouseBtn = (modules['singleClick'].options.openBackground.value) ? 1 : 0;
if (e.button != 2) {
// check if it's a relative link (no http://domain) because chrome barfs on these when creating a new tab...
var thisLink = $(this).parent().parent().parent().find('a.title').attr('href');
var thisLinkElem = $(this).parent().parent().parent().find('a.title');
//Handle the hrefOverride
var thisLink = thisLinkElem.prop('hrefOverride') || thisLinkElem.attr('href');
if (!(thisLink.match(/^http/i))) {
thisLink = 'http://' + document.domain + thisLink;
}
Expand Down Expand Up @@ -8324,11 +8326,22 @@ required type:
'IMAGE' for single images | 'GALLERY' for image galleries
required src:
if type is IMAGE then src is an image URL string
if type is GALLERY then src is an array of URL strings
if type is GALLERY then src is an array javascript objects of the form:
{
required src:
the URL of the image
optional title:
string to be displayed directly above the image (per image)
optional caption:
string to be displayed directly below the image (per image)
}

optional hrefOverride:
url to be followed when clicking or opening, this is a consession to the history system.
optional imageTitle:
string to be dispalyed above the image.
string to be dispalyed above the image (per gallery)
optional caption:
string to be displayed below the image
string to be displayed below the image (per gallery)
optional credits:
string to be displayed below caption
*/
Expand Down Expand Up @@ -8711,6 +8724,22 @@ modules['showImages'] = {
if (!elem) return false;
var href = elem.href;
if (!href) return false;
if (elem.hrefOverride) {
elem.addEventListener('mouseover', function(e) {
if (!this.hrefSwitched) {
this.savedHref = this.href;
this.href = this.hrefOverride;
this.hrefSwitched = true;
}
});
elem.addEventListener('mouseout', function(e) {
if (this.hrefSwitched && this.savedHref) {
this.href = this.savedHref;
this.hrefSwitched = false;
}
});
}

//This should not be reached in the case of duplicates
elem.name = 'img'+this.imagesRevealed[href];
this.imageList.push(elem);
Expand Down Expand Up @@ -8809,7 +8838,7 @@ modules['showImages'] = {

var imageAnchor = document.createElement('a');
addClass(imageAnchor, 'madeVisible');
imageAnchor.href = imageLink.href;
imageAnchor.href = imageLink.hrefOverride || imageLink.href;
if (this.options.openInNewWindow.value) {
imageAnchor.target = '_blank';
}
Expand Down Expand Up @@ -8866,7 +8895,7 @@ modules['showImages'] = {

var imageAnchor = document.createElement('a');
addClass(imageAnchor, 'madeVisible');
imageAnchor.href = imageLink.href;
imageAnchor.href = imageLink.hrefOverride || imageLink.href;
if (this.options.openInNewWindow.value) {
imageAnchor.target ='_blank';
}
Expand Down Expand Up @@ -8976,7 +9005,7 @@ modules['showImages'] = {
trackImageLoad: function(link, image) {
if (modules['showImages'].options.markVisited.value) {
image.addEventListener('load', function(e) {
var url = link.historyURL || link.href;
var url = link.savedHref || link.href;
addClass(link, 'visited');
modules['showImages'].imageTrackStack.push(url);
if (modules['showImages'].imageTrackStack.length == 1) setTimeout(modules['showImages'].imageTrackShift, 300);
Expand Down Expand Up @@ -9123,7 +9152,6 @@ modules['showImages'] = {
handleInfo: function(elem, info) {
elem.type = info.type;
elem.src = info.src;
elem.href = info.src;
modules['showImages'].createImageExpando(elem);
}
},
Expand Down Expand Up @@ -9219,6 +9247,8 @@ modules['showImages'] = {
caption: e.image.caption
};
});
elem.hrefOverride = elem.href;
elem.href = elem.src[0].src;
elem.type = 'GALLERY';
modules['showImages'].createImageExpando(elem);
}
Expand Down Expand Up @@ -9336,6 +9366,8 @@ modules['showImages'] = {
elem.src = {
src: info.ITEMS_GALLERY
};
elem.hrefOverride = elem.href;
elem.href = info.ITEMS_GALLERY[0];
} else {
elem.type = 'IMAGE';
elem.href = info.ITEMS_GALLERY[0];
Expand Down Expand Up @@ -9366,6 +9398,7 @@ modules['showImages'] = {
handleInfo: function(elem, info) {
elem.type = 'IMAGE';
elem.src = info.src;
elem.hrefOverride = elem.href;
elem.href = info.src;
modules['showImages'].revealImageDeferred(elem);
}
Expand Down Expand Up @@ -9458,9 +9491,11 @@ modules['showImages'] = {
elem.imageTitle = info.title;
if(['jpg', 'jpeg', 'png', 'gif'].indexOf(info.url)) {
elem.src = info.url;
elem.hrefOverride = elem.href;
elem.href = info.url;
} else {
elem.src = info.thumbnail_url;
elem.hrefOverride = elem.href;
elem.href = info.thumbnail_url;
}
elem.credits = 'Art by: <a href="'+info.author_url+'">'+info.author_name+'</a> @ DeviantArt';
Expand Down Expand Up @@ -9505,6 +9540,7 @@ modules['showImages'] = {
},
handleInfo: function(elem, info) {
var post = info.response.posts[0];
elem.hrefOverride = elem.href;
switch (post.type) {
case 'photo':
if (post.photos.length > 1) {
Expand All @@ -9515,9 +9551,7 @@ modules['showImages'] = {
caption: e.caption,
};
});
elem.href = post.photos.map(function(elem) {
return elem.original_size.url;
});
elem.href = post.photos[0].original_size.url;
} else {
elem.type = "IMAGE";
elem.src = post.photos[0].original_size.url;
Expand Down

0 comments on commit e5d7b75

Please sign in to comment.