Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: sh.reddit.com search and compact view not loading #1452

Merged
merged 31 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
a568dd6
Temp fix to show thumbnails on mouseover
LiliaDoe Nov 1, 2024
855e950
Fix for some gifs not loading
LiliaDoe Nov 3, 2024
46d2fa6
Fix searches not zooming image
LiliaDoe Nov 3, 2024
8b46182
Video posts in search will now show their thumbnail
LiliaDoe Nov 3, 2024
0b1a811
Clarify things
LiliaDoe Nov 4, 2024
e492847
Looking into way to load video in search and compact
LiliaDoe Nov 4, 2024
20975b1
Update variable scopes
LiliaDoe Nov 4, 2024
885fb9a
Remove slow function
LiliaDoe Nov 4, 2024
12276f0
Fix galleries and videos in sh.reddit
LiliaDoe Nov 4, 2024
10fde22
Update variable scopes
LiliaDoe Nov 4, 2024
0da8a21
Clean up old code
LiliaDoe Nov 4, 2024
de06857
Redo search handler
LiliaDoe Nov 4, 2024
c0c993f
Fix galleries in searches
LiliaDoe Nov 4, 2024
ea921ba
Optimize selector function
LiliaDoe Nov 4, 2024
7c6c324
Condense repeated code
LiliaDoe Nov 4, 2024
66ec118
Clarify comment
LiliaDoe Nov 5, 2024
e3721f4
Clean up sh.reddit search link code
LiliaDoe Nov 5, 2024
262cf2c
Make images load by hovering over thumbnail in thumbnail view
LiliaDoe Nov 5, 2024
8aa804b
Rearrange selector blocks to combine sh.reddit selectors
LiliaDoe Nov 5, 2024
59971c5
Update reddit.js
LiliaDoe Nov 5, 2024
315a102
Make videos in sh.reddit compact view only play when hover on thumbnail
LiliaDoe Nov 5, 2024
bc58d5e
Fix videos loading while hovering anywhere on post
LiliaDoe Nov 5, 2024
ed9f48b
Fix galleries in sh.reddit compact view not loading
LiliaDoe Nov 5, 2024
4c9fbc2
fix variables in old.reddit galleries
LiliaDoe Nov 5, 2024
888af1a
Fix comment mistake
LiliaDoe Nov 5, 2024
0c6b4ff
Change variable names for clarity
LiliaDoe Nov 5, 2024
0bfc7d5
Make similar gallery selector blocks into a switch statement
LiliaDoe Nov 5, 2024
32f8892
Revert previous commit
LiliaDoe Nov 5, 2024
a047f57
Fix gallery variable being changed from master
LiliaDoe Nov 6, 2024
99139d4
Minor style changes
LiliaDoe Nov 6, 2024
01892cf
Fix switch case ending with unconditional break
LiliaDoe Nov 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 96 additions & 32 deletions plugins/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ hoverZoomPlugins.push({
}

$('.hoverZoomLink').each(function () {
var _this = $(this);
let _this = $(this);
if (options.filterNSFW && _this.parents('.over18').length) {
_this.removeClass('hoverZoomLink');
}
Expand Down Expand Up @@ -88,19 +88,72 @@ hoverZoomPlugins.push({
}
});

// To load sh.reddit images
$('img.i18n-post-media-img').one('mouseover', function () {
// To load Image from thumbnail in searches in sh.reddit
$('faceplate-tracker[data-faceplate-tracking-context*="post_thumbnail"]').each(function () {
let img = $(this);
let url = this.children[0].href;
chrome.runtime.sendMessage({action:'ajaxRequest', url: url, method: 'GET'}, function(data) {
let doc = document.implementation.createHTMLDocument();
doc.body.innerHTML = data;

let post = $(doc.querySelector('shreddit-post'));
let link = post.attr('content-href');
let type = post.attr('post-type');
if (link.search('.gifv') != -1) { type = 'video' }

switch (type) {
case 'video': {
let title = post.attr('post-title');
img.data('hoverZoomSrc', [link + '/DASH_480.mp4', link + '/DASH_360.mp4']);
if (link.search('.gifv') != -1) {
img.data('hoverZoomSrc', [link.replace(/\.gifv?/, '.mp4'), link.replace(/\.gifv?/, '.webm')]);
}
img.data('hoverZoomCaption', [title]);
img.addClass('hoverZoomLink');
return
}
case 'gallery': {
let galleryid = post.attr('id');
$.get('https://www.reddit.com/by_id/' + galleryid + '.json?raw_json=1', data => processGalleryResponse(img, data));
return
}
case 'link':
link = post.find('img[src*="external-preview.redd.it"]:first').attr('src');
break
default:
break
}
hoverZoom.prepareLink(img, link);
});
});

// Supports images in sh.reddit compact view
$('shreddit-post[content-href*="//i.redd.it"]').one('mouseover', function () {
let post = $(this);
let link = post.attr('src');
hoverZoom.prepareLink(post, link)
let link = post.attr('content-href');
let thumbnail = post.find('div[slot*="thumbnail"]:first-child');

hoverZoom.prepareLink(thumbnail, link);
});

// To load sh.reddit videos
// Supports images in sh.reddit card view
$('img.i18n-post-media-img').one('mouseover', function () {
let post = $(this);
let link = post.attr('content-href');

hoverZoom.prepareLink(post, link);
});

// Supports videos in sh.reddit card view
$('shreddit-player-2').one('mouseover', function () {
let post = $(this);
let src = post.attr('src');
let packagedMedia = post.attr('packaged-media-json');
let link = 'https://' + packagedMedia.match(/"source":{"url":".*(packaged-media.redd.it\/.*)","dimensions"/)[1];
hoverZoom.prepareLink(post, link);

if (packagedMedia) {
src = 'https://' + packagedMedia.match(/"source":{"url":".*(packaged-media.redd.it\/.*)","dimensions"/)[1];
}
hoverZoom.prepareLink(post, src);
});

var promises = [];
Expand Down Expand Up @@ -138,70 +191,81 @@ hoverZoomPlugins.push({
}

$('div[data-url*="//www.reddit.com/gallery/"]').one('mouseover', function () {
var post = $(this);
let post = $(this);
if (post.data().hoverZoomMouseOver) return;
post.data().hoverZoomMouseOver = true;
var link = post.attr('data-url');
let link = post.attr('data-url');
var galleryid = link.substring(link.lastIndexOf('/') + 1);
$.get('https://www.reddit.com/by_id/t3_' + galleryid + '.json?raw_json=1', data => processGalleryResponse(post, data));
});

$('a[href*="//www.reddit.com/gallery/"]').one('mouseover', function () {
var post = $(this);
let post = $(this);
if (post.data().hoverZoomMouseOver) return;
post.data().hoverZoomMouseOver = true;
var link = post.attr('href');
var galleryid = link.substring(link.lastIndexOf('/') + 1);
let link = post.attr('href');
let galleryid = link.substring(link.lastIndexOf('/') + 1);
$.get('https://www.reddit.com/by_id/t3_' + galleryid + '.json?raw_json=1', data => processGalleryResponse(post, data));
});

$('div[data-is-gallery=true]').one('mouseover', function () {
var post = $(this);
let post = $(this);
if (post.data().hoverZoomMouseOver) return;
post.data().hoverZoomMouseOver = true;
var galleryid = post.attr('data-fullname');
let galleryid = post.attr('data-fullname');
$.get('https://www.reddit.com/by_id/' + galleryid + '.json?raw_json=1', data => processGalleryResponse(post, data));
});

// supports sh.reddit compact view galleries
$('shreddit-post[content-href*="//www.reddit.com/gallery/"]').one('mouseover', function () {
let post = $(this);
if (post.data().hoverZoomMouseOver) return;
post.data().hoverZoomMouseOver = true;
let galleryid = post.attr('id');
let thumbnail = post.find('div[slot*="thumbnail"]:first-child'); //finds thumbnail
$.get('https://www.reddit.com/by_id/' + galleryid + '.json?raw_json=1', data => processGalleryResponse(thumbnail, data));
});

// supports sh.reddit card view galleries
$('gallery-carousel').one('mouseover', function () {
var post = $(this);
let post = $(this);
if (post.data().hoverZoomMouseOver) return;
post.data().hoverZoomMouseOver = true;
var galleryid = post.attr('post-id');
let galleryid = post.attr('post-id');
$.get('https://www.reddit.com/by_id/' + galleryid + '.json?raw_json=1', data => processGalleryResponse(post, data));
});

$('div[data-url*="//v.redd.it/"]').each(function () {
var post = $(this);
var link = post.attr('data-url');
var title = post.find('a.title').text();

post.find('a.thumbnail,a.title').each(function() {
var img = $(this);

$('div[data-url*="//v.redd.it/"], shreddit-post[content-href*="//v.redd.it/"], shreddit-post[content-href*="https://i.imgur.com/"]').each(function () {
let post = $(this);
let link = post.attr('data-url') || post.attr('content-href');
let title = post.find('a.title').text() || post.attr('post-title');
let hoverTargets = post.attr('data-url') ? 'a.thumbnail,a.title' : 'div[slot*="thumbnail"]:first-child'
post.find(hoverTargets).each(function() {
let img = $(this);
// Use /DASH_600_K as a default if for any reason the ajax request below doesn't find a valid link
img.data('hoverZoomSrc', [link + '/DASH_600_K']);
// In case of imgur link, replace .gifv with .mp4 or .webm
img.data('hoverZoomSrc', [link + '/DASH_600_K',link.replace(/\.gifv?/, '.mp4'),link.replace(/\.gifv?/, '.webm')]);
img.data('hoverZoomCaption', [title]);

promises.push(new Promise(function (resolve, reject) {
chrome.runtime.sendMessage(
{ action:'ajaxRequest', url: link + '/DASHPlaylist.mpd', method: 'GET' },
function (xml) {
try {
var xmlDoc = (new DOMParser()).parseFromString(xml, 'application/xml');
var highestRes = [].slice.call(xmlDoc.querySelectorAll('Representation[frameRate]'))
let xmlDoc = (new DOMParser()).parseFromString(xml, 'application/xml');
let highestRes = [].slice.call(xmlDoc.querySelectorAll('Representation[frameRate]'))
.sort(function (r1, r2) {
var w1 = parseInt(r1.getAttribute('width')), w2 = parseInt(r2.getAttribute('width'));
let w1 = parseInt(r1.getAttribute('width')), w2 = parseInt(r2.getAttribute('width'));
return w1 > w2 ? -1 : (w1 < w2 ? 1 : 0);
})
.find(function (repr) { return !!repr.querySelector('BaseURL'); });

if (highestRes) {
var baseUrl = highestRes.querySelector('BaseURL').textContent.trim();
let baseUrl = highestRes.querySelector('BaseURL').textContent.trim();
img.data('hoverZoomSrc', [baseUrl.indexOf('//') !== -1 ? baseUrl : link + '/' + baseUrl]);
}

var audio = xmlDoc.querySelector('Representation[audioSamplingRate]'),
let audio = xmlDoc.querySelector('Representation[audioSamplingRate]'),
audioUrl = audio ? audio.querySelector('BaseURL').textContent.trim() : undefined;
if (audioUrl) {
img.data('hoverZoomAudioSrc', [audioUrl.indexOf('//') !== -1 ? audioUrl : link + '/' + audioUrl]);
Expand Down