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

#2602 #2604

Merged
merged 7 commits into from
Jan 14, 2025
Merged

#2602 #2604

Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions src/_locales/en/adnauseam.json
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@
"message": "Blur Collected ads",
"description": "English: Blur Collected ads"
},
"hideDeadAds": {
"message": "Hide Dead ads",
"description": "English: Hide Dead ads"
},
"showAdsDebugPrompt": {
"message": "Show ads for ad-collection debugging",
"description": "English: Show ads for ad-collection debugging"
Expand Down
14 changes: 12 additions & 2 deletions src/css/vault.css
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,8 @@ div.bullets { display: none; } /* hide by default */
pointer-events: none;
}

#stage.blur .ad img {
filter: blur(5px);
#stage.blur .item:not(.inspected) .ad img {
filter: blur(10px);
}

#stage.blur .ad img:hover {
Expand All @@ -820,6 +820,16 @@ div.bullets { display: none; } /* hide by default */
background: white;
}

#svg-progress {
position: absolute;
display: none;
top: 40px;
left: 50%;
transform: translateX(-50%);
color: black;
z-index: 1000;
}

/*
.item, .item img {
//temporary fix for the big images bug
Expand Down
23 changes: 15 additions & 8 deletions src/js/adn/adn-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,13 +362,15 @@ export const clearAds = function () {
}
};

export const purgeDeadAds = function (deadAds) {
export const purgeDeadAds = function (deadAds, callback) {
const msg = i18n$('adnPurgeConfirm');
const proceed = window.confirm(msg); // changed from vAPI.confirm merge1.14.12
if (proceed) {
vAPI.messaging.send('adnauseam', {
what: 'purgeDeadAds',
'deadAds': deadAds
}).then(data => {
callback(data);
});
}
};
Expand Down Expand Up @@ -430,11 +432,11 @@ export const decodeEntities = (function () {

/********* advault capture feature *********/

export async function generateCaptureSvg (jsonData) {
export async function generateCaptureSvg (jsonData, currentZoom, $loaded, $total) {
// create hidden div to hold the SVG
const output = document.createElement('div');
output.style.display = 'none';
const svgContent = await generateSVG(jsonData);
const svgContent = await generateSVG(jsonData, currentZoom, $loaded, $total);
output.innerHTML = svgContent;
// Make the SVG downloadable
const blob = new Blob([svgContent], { type: 'image/svg+xml' });
Expand All @@ -460,7 +462,7 @@ export async function fetchImageAsBase64(url) {
});
}

export async function generateSVG(json) {
export async function generateSVG(json, currentZoom, $loaded, $total) {
if (!json.ads || !Array.isArray(json.ads)) {
throw new Error('Invalid JSON structure. Expected an "ads" array.');
}
Expand All @@ -470,19 +472,24 @@ export async function generateSVG(json) {

console.log("Adn Capture: Total images", json.ads.length);
var imageCounter = 0;

$total.parent().show();
$total.text(json.ads.length);
// Fetch and encode all image URLs to Base64
const images = await Promise.all(
json.ads.map(async (ad) => {
let src = ad.src;
if (src.startsWith('http')) {
src = await fetchImageAsBase64(src);
}
console.log("Parsed image", ad);
//console.log("Parsed image", ad);
imageCounter++
console.log("Adn Capture: Image", imageCounter, "of", json.ads.length);

return `<image href="${src}" x="${ad.pos.x}" y="${ad.pos.y}" height="${ad.height}" width="${ad.width}" />`;
$loaded.text(imageCounter);
let x = ad.pos.x * currentZoom;
let y = ad.pos.y * currentZoom;
let w = ad.width * currentZoom;
let h = ad.height * currentZoom;
return `<image href="${src}" x="${x}" y="${y}" height="${h}" width="${w}" />`;
})
);

Expand Down
49 changes: 48 additions & 1 deletion src/js/adn/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ const adnauseam = (function () {
};

const purgeDeadAdsAdmap = function (deadAds) {
let deadIds = deadAds.map(deadad => deadad.children.map(c => c.id))[0]
let deadIds = deadAds.map(deadad => deadad.children.map(c => c.id)[0])
const pages = Object.keys(admap);
for (let i = 0; i < pages.length; i++) {
if (admap[pages[i]]) {
Expand Down Expand Up @@ -963,6 +963,31 @@ const adnauseam = (function () {
storeAdData();
}

const deadAd = function (ad, setDead) {
console.log("deadAd", ad)
if (!ad) {
return warn("No Ad to set Dead", id, admap);
}

const pageHash = YaMD5.hashStr(ad.pageUrl);
if (pageHash !== YaMD5.hashStr("")) {
const hash = computeHash(ad);
if (admap[pageHash][hash]) {
let addata = admap[pageHash][hash];
if(setDead) { // set ad as dead
if (admap[pageHash][hash]["dead"]) {
admap[pageHash][hash]["dead"] = parseInt(admap[pageHash][hash]["dead"]) + 1;
} else {
admap[pageHash][hash]["dead"] = 1;
}
} else { // set as not dead
admap[pageHash][hash]["dead"] = 0;
}
storeAdData();
}
}
}

const adsForUI = function (pageUrl) {
return {
data: adlist(pageUrl, false, true),
Expand Down Expand Up @@ -1864,6 +1889,18 @@ const adnauseam = (function () {
request.ids.forEach(deleteAd);
};


exports.deadAd = function (request, pageStore, tabId) {
let ad = request.ad;
deadAd(ad, true);
};

exports.notDeadAd = function (request, pageStore, tabId) {
let ad = request.ad;
deadAd(ad, false);
};


exports.logAdSet = function (request, pageStore, tabId) {

let data = '';
Expand Down Expand Up @@ -2286,6 +2323,11 @@ const adnauseam = (function () {
exports.getBlurCollectedAds = function () {
return µb.userSettings.blurCollectedAds;
};

// check if "blur collected ads" options is enabled or not
exports.getHideDeadAds = function () {
return µb.userSettings.hideDeadAds;
};

// ADN broadcast change of "disable warning" to all tabs
exports.setWarningDisabled = function () {
Expand Down Expand Up @@ -2348,6 +2390,11 @@ const adnauseam = (function () {
return json;
};

exports.purgeDeadAds = function (request, pageStore, tabId) {
purgeDeadAdsAdmap(request.deadAds);
return adsForUI();
};

return exports;
})();

Expand Down
75 changes: 59 additions & 16 deletions src/js/adn/vault.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ var transitionTimeout = null
let gAds, gAdSets, gMin, gMax, gSliderRight, gSliderLeft, settings, pack;
let lastAdDetectedTime, waitingAds = []; // stateful

var hideDeadAds = false;

onBroadcast(request => {
//console.log("GOT BROADCAST", request);
switch (request.what) {
Expand Down Expand Up @@ -128,12 +130,15 @@ onBroadcast(request => {

/********************************************************************/

const renderAds = function (json) {

const renderAds = function (json, purge) {
gAds = json.data; // store
addInterfaceHandlers();
settings = json.prefs;
createSlider();
if (purge) {
createSlider("update");
} else {
createSlider();
}
setCurrent(json.current);

vAPI.messaging.send(
Expand Down Expand Up @@ -168,14 +173,21 @@ const renderAds = function (json) {
'adnauseam', {
what: 'getBlurCollectedAds'
}).then(blurCollectedAds => {
//console.log("blurCollectedAds", blurCollectedAds);
if (blurCollectedAds) {
uDom("#stage").addClass('blur');
} else {
uDom("#stage").removeClass('blur');
}
});

vAPI.messaging.send(
'adnauseam', {
what: 'getHideDeadAds'
}).then(_hideDeadAds => {
console.log("_hideDeadAds" ,_hideDeadAds)
hideDeadAds = _hideDeadAds;
});

if (settings.devMode || settings.logEvents) {
console.log("DevMode: enabling capture button");
$('#capture').on('click', onCapture);
Expand Down Expand Up @@ -718,9 +730,28 @@ function appendDisplayTo($div, adset) {
var failedCount = adset.failedCount();
var dntCount = adset.dntCount();
var visitedCount = adset.visitedCount();
var deadCount = adset.deadCount();
var foundTs = adset.foundTs();
var w = adset.width();
var h = adset.height();
let img_src = adset.child(0).contentData.src;

if (deadCount > 0 && hideDeadAds) {
// still try to load the image in case it is not dead
let img = new Image();
img.src = img_src;
img.onload = function () {
messager.send('adnauseam', {
what: 'notDeadAd',
ad: adset.children[0]
});
}


// dont display add
return;
}


var hasSize = w && h;

Expand Down Expand Up @@ -769,7 +800,6 @@ function appendDisplayTo($div, adset) {

// add white background to transparent ads that are saved data strings
// https://github.com/dhowe/AdNauseam/issues/1978
let img_src = adset.child(0).contentData.src;
var isPNGdata = img_src.includes('data:image/png');
var cl = isPNGdata ? "white-bg" : "";
const $img = $('<img/>', {
Expand All @@ -787,11 +817,16 @@ function appendDisplayTo($div, adset) {
$img.on("error", function () {
isLoaded = true;
setItemClass($div, 'image-error');
$img.attr('src', 'img/placeholder.svg');
$img.attr('src', ' ');
$img.attr('alt', 'Unable to load image');
$img.attr('data-error', 'error');
$img.off("error");
$div.addClass('loaded');
// tell the addon
messager.send('adnauseam', {
what: 'deadAd',
ad: adset.children[0]
});
});

$img.on('load', function () {
Expand Down Expand Up @@ -1261,7 +1296,9 @@ function toggleInterface() {

$("body")
.css('background-image', 'none')
.css({ 'background-color': '#fff' });
.css({ 'background-color': '#fff'})
.css({ 'pointer-events': 'none'});

ifs.forEach(s => $(s).hide());

// remove all duplicate classes (TODO: just hide them)
Expand All @@ -1272,7 +1309,8 @@ function toggleInterface() {
} else {
$("body")
.css('background-image', 'url(../img/gray_grid.png)')
.css({ 'background-color': '#000' });
.css({ 'background-color': '#000' })
.css({ 'pointer-events': 'all'});
ifs.forEach(s => $(s).show());
}
}
Expand Down Expand Up @@ -1986,7 +2024,7 @@ function createSlider(mode) {
vaultLoading = false;
break;
case "resize":
repack();
// repack();
runFilter([gMin, gMax])
break;
case "update":
Expand Down Expand Up @@ -2119,7 +2157,6 @@ function parsePackElements(packElements, gMin, gMax) {
}

function onCapture() { // save screenshot

let dbug = true;
if (dbug) console.log('onCapture');
toggleInterface(showInterface = true);
Expand Down Expand Up @@ -2175,7 +2212,7 @@ function onCapture() { // save screenshot
meta: meta
}

generateCaptureSvg(capture).then(svgUrl => {
generateCaptureSvg(capture, userZoomScale/100, $("#svg-loaded"), $("#svg-total")).then(svgUrl => {

let exportData = JSON.stringify(capture, null, ' ')
let filename = getExportFileName();
Expand All @@ -2195,6 +2232,8 @@ function onCapture() { // save screenshot
'filename': filename
});

$("#svg-progress").hide();

const screenshot = new Image();
screenshot.src = imgUrl;
screenshot.onload = () => {
Expand All @@ -2212,17 +2251,16 @@ function onCapture() { // save screenshot
function onPurgeDeadAds() {
let deadAds = getDeadAds()
if (deadAds.length > 0) {
purgeDeadAds(getDeadAds())
purgeDeadAds(getDeadAds(), function (response) {
renderAds(response, true)
})
} else {
console.log("no dead ads to purge")
}
}

function getDeadAds() {
let adsGids = [];
document.querySelectorAll(".image-error")
.forEach(el => adsGids.push(parseInt(el.getAttribute('data-gid'))));
return gAdSets.filter(adset => adsGids.includes(adset.gid))
return gAdSets.filter(adset => adset.deadCount() > 0)
}

/********************************************************************/
Expand Down Expand Up @@ -2297,6 +2335,11 @@ AdSet.prototype.failedCount = function () {
return this.children.filter((d) => containerObj.state(d) === 'failed').length;
};

AdSet.prototype.deadCount = function () {
const containerObj = this;
return this.children[0]?.dead;
};

AdSet.prototype.dntCount = function () {
const containerObj = this;
return this.children.filter((d) => containerObj.state(d) === 'dnt-allowed').length;
Expand Down
3 changes: 2 additions & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ const userSettingsDefault = {
clickProbability: 1.0,
removeAdsInPrivate: true,
strictBlockingMode: false,
disableWarnings: false,
disableWarnings: false,
blurCollectedAds: false,
hideDeadAds: true,

clickOnlyWhenIdleFor: 0,
noIncomingCookies: true,
Expand Down
Loading