Skip to content

Commit

Permalink
Update for "ban image" functionality (extesy#1516)
Browse files Browse the repository at this point in the history
- when an image (or video & audio track) is banned:
   - url of banned image is stored in background page local storage
   - a message is sent to all tabs with updated list of banned urls
- removal of async call to background page: check for banned image is now performed synchronously in each tab
  • Loading branch information
GrosPoulet committed Jan 5, 2025
1 parent 5671342 commit a2668bc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 48 deletions.
56 changes: 34 additions & 22 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function downloadFile(url, filename, conflictAction, callback) {
function onMessage(message, sender, callback) {
switch (message.action) {
case 'downloadFileBlob':
/**
/**
* direct URL download through Chrome API might be prohibited (e.g: Pixiv)
* workaround:
* 1. obtain ArrayBuffer from XHR request (GET URL)
Expand All @@ -89,7 +89,7 @@ function onMessage(message, sender, callback) {
*/

/*
* Workaround for permissions.request not returning a promise in Firefox
* Workaround for permissions.request not returning a promise in Firefox
* First checks if permissions are availble. If true, downloads file. If not, requests them.
* Not as clean or effecient as just using 'permissions.request'.
*/
Expand All @@ -111,7 +111,7 @@ function onMessage(message, sender, callback) {
case 'downloadFile':
cLog('downloadFile: ' + message);
/*
* Workaround for permissions.request not returning a promise in Firefox
* Workaround for permissions.request not returning a promise in Firefox
* First checks if permissions are availble. If true, downloads file. If not, requests them.
* Not as clean or effecient as just using 'permissions.request'.
*/
Expand Down Expand Up @@ -212,9 +212,9 @@ function onMessage(message, sender, callback) {
case 'resetBannedImages':
resetBannedImages();
break;
case 'isImageBanned':
callback(isImageBanned(message));
return true;
case 'sendBannedImages':
sendBannedImages();
break;
}
}

Expand Down Expand Up @@ -374,7 +374,7 @@ function updateHeaders(headers, settings) {
return headers;
}

/**
/**
* add listeners for web requests:
* - onBeforeSendHeaders
* - onHeadersReceived
Expand All @@ -398,7 +398,7 @@ function addWebRequestListeners() {
chrome.webRequest.OnSendHeadersOptions.EXTRA_HEADERS,
].filter(Boolean));
}

}

/**
Expand All @@ -419,6 +419,7 @@ function removeWebRequestListeners() {
// add url of image, video or audio track to ban list so it will not be zoomed again
function banImage(message) {
const url = message.url;
if (!url) return;

// store urls to ban in background page local storage so theys are shared by all pages & will survive browser restart
let bannedUrls = localStorage.getItem('HoverZoomBannedUrls') || '{}';
Expand All @@ -429,27 +430,38 @@ function banImage(message) {
bannedUrls[url] = { 'location' : message.location };
update = true;
}
if (update) localStorage.setItem('HoverZoomBannedUrls', JSON.stringify(bannedUrls));
if (update) {
localStorage.setItem('HoverZoomBannedUrls', JSON.stringify(bannedUrls));
// send updated list to tabs
sendBannedImages();
}
} catch {}
}

// clear list of banned image, video or audio track urls
function resetBannedImages() {
localStorage.removeItem('HoverZoomBannedUrls');
function resetBannedImages() {
localStorage.setItem('HoverZoomBannedUrls', '{}');
// send updated list to tabs
sendBannedImages();
}

// check if url of image, video or audio track belongs to ban list
function isImageBanned(message) {
const url = message.url;
let bannedUrls = localStorage.getItem('HoverZoomBannedUrls') || '{}';
// send list of banned image, video or audio track urls to all tabs
function sendBannedImages() {
let list = localStorage.getItem('HoverZoomBannedUrls') || '{}';
try {
bannedUrls = JSON.parse(bannedUrls);
} catch { return false; }
if (!bannedUrls[url]) {
return false;
} else {
return true;
}
list = JSON.parse(list);
} catch { return; }

var request = {action:'bannedImagesListChanged', 'list':list};
chrome.windows.getAll(null, function (windows) {
for (var i = 0; i < windows.length; i++) {
chrome.tabs.query({windowId: windows[i].id}, function (tabs) {
for (var j = 0; j < tabs.length; j++) {
chrome.tabs.sendMessage(tabs[j].id, request);
}
});
}
});
}

init();
71 changes: 45 additions & 26 deletions js/hoverzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function Logger() {
var hoverZoom = {

options:{},
bannedImagesList:{},
currentLink:null,
hzViewer:null,
hzLoader:null,
Expand Down Expand Up @@ -434,13 +435,13 @@ var hoverZoom = {
if (hzAbove) {
hzAbove.css('max-width', imgFullSize[0].clientWidth);
hzAbove.css('top', options.abovePositionOffset + '%');
if (options.abovePositionOffset != 0)
if (options.abovePositionOffset != 0)
hzAbove.css('position', 'absolute');
}
if (hzBelow) {
hzBelow.css('max-width', imgFullSize[0].clientWidth);
hzBelow.css('bottom', options.belowPositionOffset + '%');
if (options.belowPositionOffset != 0)
if (options.belowPositionOffset != 0)
hzBelow.css('position', 'absolute');
}

Expand Down Expand Up @@ -515,7 +516,7 @@ var hoverZoom = {
} else if (fullZoomKey) {
// naturalWidth replaced with wndWidth to make image fill window
// offset subtracted to keep image within window's bounds
imgFullSize.width(wndWidth - offset - padding - 2 * scrollBarWidth);
imgFullSize.width(wndWidth - offset - padding - 2 * scrollBarWidth);
} else if (fullZoom) {
imgFullSize.width(Math.min(srcDetails.naturalWidth * zoomFactor, wndWidth - offset - padding - 2 * scrollBarWidth));
} else if (displayOnRight) {
Expand Down Expand Up @@ -1066,26 +1067,24 @@ var hoverZoom = {

srcDetails.url = src;
srcDetails.audioUrl = audioSrc;
getVideoAudioSubtitlesFromUrl();

clearTimeout(loadFullSizeImageTimeout);

// if the action key has been pressed over an image, no delay is applied
const delay = actionKeyDown || explicitCall ? 0 : (isVideoLink(srcDetails.url) ? options.displayDelayVideo : options.displayDelay);
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);

// Temporarily removing until a better fix is found: sendMessage is async so it can't be used to set local variables
/*if (audioSrc) {
chrome.runtime.sendMessage({action:'isImageBanned', url:audioSrc}, function (result) {
if (!result) {
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);
}
});
} else if (src) {
chrome.runtime.sendMessage({action:'isImageBanned', url:src}, function (result) {
if (!result) {
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);
}
});
}*/



if (srcDetails.audioUrl) {
if (!isImageBanned(srcDetails.audioUrl)) {
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);
}
} else if (srcDetails.url) {
if (!isImageBanned(srcDetails.url)) {
loadFullSizeImageTimeout = setTimeout(loadFullSizeImage, delay);
}
}

loading = true;
}
Expand All @@ -1107,7 +1106,7 @@ var hoverZoom = {
// for if user releases mouse button before timer goes off
let shortPressRight = false;
let shortPressMiddle = false;

function documentContextMenu(event) {
if (!preventDefaultContext || hideKeyDown) {
hideKeyDown = false; // releases hideKey if it was held down
Expand All @@ -1121,7 +1120,7 @@ var hoverZoom = {
event.preventDefault();
}
}

function preventDefaultMouseAction(disableDefault, button){
switch (button) {
case -1:
Expand Down Expand Up @@ -1319,7 +1318,7 @@ var hoverZoom = {
return;
default:
// The following only trigger when image is displayed
if (imgFullSize) {
if (imgFullSize) {
switch (mouseButtonKey) {
case options.lockImageKey:
case options.copyImageKey:
Expand All @@ -1344,7 +1343,7 @@ var hoverZoom = {
break;
default:
// The following only trigger when image is displayed
if (imgFullSize) {
if (imgFullSize) {
switch (mouseButtonKey) {
case options.lockImageKey:
case options.copyImageKey:
Expand Down Expand Up @@ -1388,7 +1387,7 @@ var hoverZoom = {
const rightButtonKey = ((shortPressRight || !options.rightShortClickAndHold) && options.rightShortClick) ? -3 : -1;
const middleButtonKey = ((shortPressMiddle || !options.middleShortClickAndHold) && options.middleShortClick) ? -4 : -2;
let mouseButtonKey = [null,middleButtonKey,rightButtonKey,null,null][event.button];

switch (mouseButtonKey) {
case options.actionKey:
if (actionKeyDown) {
Expand Down Expand Up @@ -2283,7 +2282,6 @@ var hoverZoom = {
}

function cancelSourceLoading() {
cLog('cancelSourceLoading');
loading = false;
hz.currentLink = null;
clearTimeout(loadFullSizeImageTimeout);
Expand Down Expand Up @@ -2647,12 +2645,32 @@ var hoverZoom = {
});
}

// get list of banned image, video or audio track urls
function loadBannedImages() {
chrome.runtime.sendMessage({action:'sendBannedImages'});
}

// check if url of image, video or audio track belongs to ban list
function isImageBanned(url) {
if (!url) return false;

if (!bannedImagesList[url]) {
return false;
} else {
return true;
}
}

// deals with messages sent by background.js
function onMessage(message, sender, sendResponse) {
if (message.action === 'optionsChanged') {
options = message.options;
applyOptions();
}

if (message.action === 'bannedImagesListChanged') {
bannedImagesList = message.list;
}
}

const observer = new MutationObserver(windowOnDOMMutation);
Expand Down Expand Up @@ -3631,7 +3649,7 @@ var hoverZoom = {
chrome.runtime.sendMessage({action:'banImage', url:srcDetails.url, location:window.location.href});
}
}

function saveImage() {
saveImg();
saveVideo();
Expand Down Expand Up @@ -4007,6 +4025,7 @@ var hoverZoom = {

chrome.runtime.onMessage.addListener(onMessage);
loadOptions();
loadBannedImages();

// In case we are being used on a website that removes us from the DOM, update the internal data structure to reflect this
var target = document.getElementsByTagName('html')[0];
Expand Down

0 comments on commit a2668bc

Please sign in to comment.