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: youtube & iframe util from packages/ui to main source #1884

Merged
merged 7 commits into from
Sep 19, 2024
Merged
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
1 change: 1 addition & 0 deletions extension-manifest-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"build": "node scripts/build.js",
"start": "npm run build -- --watch",
"start:update": "./scripts/update.sh",
"debug": "npm run build -- --watch --debug",
"licenses": "license-report --config=../.license-report-config.json > dist/licenses.html",
"lint": "eslint src/",
"test": "node --test && npm run lint",
Expand Down
13 changes: 7 additions & 6 deletions extension-manifest-v3/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ if (argv.target.startsWith('safari')) {
argv.target = 'safari';
}

// Add flags to manifest
if (argv.debug) manifest.debug = true;
if (argv.staging) manifest.staging = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was wondering why you "invent" the stating mode, but from the other changes it looks like it was implicitly there for some subsystems.

We have to decide how those modes should work.

Historically:

  • debug - was setting all backends to staging and was enabling some additional logging
  • staging - was recently introduced to download adblocker engines from the staging environment

It looks like we have 3 separate aspects to deal with:

  • debugging of the addon (logs, some settings like opera serp, scritplets debugging)
  • adblocker engines environment
  • backends (staging vs production)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The staging mode usually does not work. They it yourself. The engines are not updated there (I suppose we have to trigger it manually).

Firstly, there was only a debug mode, but with that issue I had to add separate flag for loading engines from staging.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, so you mean even if we downloaded staging engine pre-build, they would update to production engine in the runtime anyways?

I'm fine to keep the staging mode. Shall we then update all endpoints configuration to use stagingMode instead of debugMode?

Copy link
Collaborator Author

@smalluban smalluban Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm start -- --staging fails... as the staging CDN does not have the proper version of the engines..

For me --staging should only set the engines' endpoints - as this must be used only when we know that we pushed something to the staging CDN for engines and only engines.

Error: Engine "dnr-ads" for "669" engine version not found
    at file:///Users/dominik/Workspace/ghostery-extension/extension-manifest-v3/scripts/download-engines.js:89:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem occurs as we don't create staging engines on every adblocker release - perhaps we should. When this happen please reach out so we can schedule a CI build.


// Download adblocker engines
if (argv.staging) {
execSync('npm run download-engines -- --staging', { stdio: 'inherit' });
manifest.debug = true;
} else {
execSync('npm run download-engines', { stdio: 'inherit' });
}
execSync('npm run download-engines' + (argv.staging ? ' -- --staging' : ''), {
stdio: 'inherit',
});

execSync('npm run download-wtm-bloomfilter', { stdio: 'inherit' });
execSync('npm run download-wtm-stats', { stdio: 'inherit' });
Expand Down
4 changes: 2 additions & 2 deletions extension-manifest-v3/src/background/adblocker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as engines from '/utils/engines.js';
import * as trackerdb from '/utils/trackerdb.js';
import Request from '/utils/request.js';
import asyncSetup from '/utils/setup.js';
import { debugMode } from '/utils/debug.js';

import { tabStats, updateTabStats } from './stats.js';
import { getException } from './exceptions.js';
Expand Down Expand Up @@ -309,12 +310,11 @@ chrome.runtime.onMessage.addListener((msg, sender) => {
return false;
});

const DEBUG_SCRIPLETS = false;
async function executeScriptlets(tabId, frameId, scripts) {
// Dynamically injected scripts can be difficult to find later in
// the debugger. Console logs simplifies setting up breakpoints if needed.
let debugMarker;
if (DEBUG_SCRIPLETS) {
if (debugMode) {
debugMarker = (text) =>
`console.log('[ADBLOCKER-DEBUG]:', ${JSON.stringify(text)});`;
} else {
Expand Down
1 change: 1 addition & 0 deletions extension-manifest-v3/src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import './exceptions.js';
import './paused.js';
import './session.js';
import './stats.js';
import './notifications.js';
import './serp.js';

import './helpers.js';
Expand Down
50 changes: 50 additions & 0 deletions extension-manifest-v3/src/background/notifications.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Ghostery Browser Extension
* https://www.ghostery.com/
*
* Copyright 2017-present Ghostery GmbH. All rights reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import * as notifications from '/utils/notifications.js';

export function openNotification(tabId, id, params) {
const url = chrome.runtime.getURL(
`/pages/notifications/${id}.html` +
(params
? `?${Object.entries(params)
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
.join('&')}`
: ''),
);

chrome.tabs.sendMessage(tabId, {
action: notifications.MOUNT_ACTION,
url,
});
}

export function closeNotification(tabId) {
chrome.tabs.sendMessage(tabId, {
action: notifications.UNMOUNT_ACTION,
});
}

chrome.runtime.onMessage.addListener((msg, sender) => {
const tabId = sender.tab?.id;
if (!tabId) return;

switch (msg.action) {
case notifications.OPEN_ACTION: {
openNotification(tabId, msg.id, msg.params);
break;
}
case notifications.CLOSE_ACTION: {
closeNotification(tabId);
break;
}
}
});
19 changes: 2 additions & 17 deletions extension-manifest-v3/src/background/onboarding.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0
*/

import { observe } from '/store/options.js';
import { showOperaSerpNotification } from '/notifications/opera-serp.js';
import { isOpera } from '/utils/browser-info.js';

let done = false;

observe('onboarding', (onboarding) => {
done = onboarding.done;

if (!done && !onboarding.shownAt) {
observe('onboarding', ({ done, shownAt }) => {
if (!done && !shownAt) {
chrome.tabs.create({
url: chrome.runtime.getURL('/pages/onboarding/index.html'),
});
}
});

if (__PLATFORM__ === 'chromium' && isOpera()) {
chrome.webNavigation.onCompleted.addListener((details) => {
if (done && details.frameId === 0) {
showOperaSerpNotification(details.tabId);
}
});
}
38 changes: 36 additions & 2 deletions extension-manifest-v3/src/background/serp.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,43 @@
import { store } from 'hybrids';
import { parse } from 'tldts-experimental';

import Options, { isPaused } from '/store/options.js';
import trackersPreviewCSS from '/content_scripts/trackers-preview.css?raw';
import { getWTMStats } from '/utils/wtm-stats';

import Options, { isPaused } from '/store/options.js';

import { isSerpSupported } from '/utils/opera.js';
import { getWTMStats } from '/utils/wtm-stats.js';
import { isOpera } from '/utils/browser-info.js';

import { openNotification } from './notifications.js';

// Opera SERP notification
if (__PLATFORM__ === 'chromium' && isOpera()) {
const NOTIFICATION_DELAY = 7 * 24 * 60 * 60 * 1000; // 7 days in milliseconds
const NOTIFICATION_SHOW_LIMIT = 4;

chrome.webNavigation.onCompleted.addListener(async (details) => {
if (details.frameId !== 0 || (await isSerpSupported())) return;

const { onboarding } = await store.resolve(Options);

if (
// Onboarding is not "done"
!onboarding.done ||
// The notification was already shown maximum times
onboarding.serpShown >= NOTIFICATION_SHOW_LIMIT ||
// The notification was already shown recently
(onboarding.serpShownAt &&
Date.now() - onboarding.serpShownAt < NOTIFICATION_DELAY)
) {
return false;
}

openNotification(details.tabId, 'opera-serp');
});
}

// Trackers preview messages
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.action === 'getWTMReport') {
sendResponse({
Expand All @@ -40,6 +73,7 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
const SERP_URL_REGEXP =
/^https:[/][/][^/]*[.]google[.][a-z]+([.][a-z]+)?[/]search/;

// SERP tracking prevention and trackers preview content scripts
chrome.webNavigation.onCommitted.addListener((details) => {
if (details.url.match(SERP_URL_REGEXP)) {
store.resolve(Options).then((options) => {
Expand Down
25 changes: 20 additions & 5 deletions extension-manifest-v3/src/background/stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { order } from '@ghostery/ui/categories';
import DailyStats from '/store/daily-stats.js';
import Options, { isPaused, observe } from '/store/options.js';

import { shouldSetDangerBadgeForTabId } from '/notifications/opera-serp.js';
import { isSerpSupported } from '/utils/opera.js';

import AutoSyncingMap from '/utils/map.js';
import { getMetadata, getUnidentifiedTracker } from '/utils/trackerdb.js';
Expand Down Expand Up @@ -50,12 +50,27 @@ observe('terms', async (terms) => {
}
});

async function hasAccessToPage(tabId) {
try {
await chrome.scripting.insertCSS({ target: { tabId }, css: '' });
return true;
} catch (e) {
return false;
}
}

async function refreshIcon(tabId) {
const options = await store.resolve(Options);

if (__PLATFORM__ === 'chromium' && isOpera() && options.terms) {
shouldSetDangerBadgeForTabId(tabId).then((danger) => {
setBadgeColor(danger ? '#f13436' /* danger-500 */ : undefined);
isSerpSupported().then(async (supported) => {
if (!supported) {
setBadgeColor(
(await hasAccessToPage(tabId))
? undefined
: '#f13436' /* danger-500 */,
);
}
});
}

Expand All @@ -78,7 +93,7 @@ async function refreshIcon(tabId) {
// Note: Even in MV3, this is not (yet) returning a promise.
chromeAction.setIcon({ tabId, ...data }, () => {
if (chrome.runtime.lastError) {
console.debug(
console.error(
'setIcon failed for tabId',
tabId,
'(most likely the tab was closed)',
Expand All @@ -94,7 +109,7 @@ async function refreshIcon(tabId) {
text: options.trackerCount ? String(stats.trackers.length) : '',
});
} catch (e) {
console.debug('Error while trying update the badge', e);
console.error('Error while trying update the badge', e);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions extension-manifest-v3/src/background/telemetry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import { store } from 'hybrids';

import Options, { observe } from '/store/options.js';
import { debugMode } from '/utils/debug.js';

import Telemetry from './metrics.js';

Expand Down Expand Up @@ -91,10 +92,10 @@ chrome.runtime.onMessage.addListener((msg) => {

(async () => {
const storage = await loadStorage();
const { version, debug } = chrome.runtime.getManifest();
const { version } = chrome.runtime.getManifest();

telemetry = new Telemetry({
METRICS_BASE_URL: debug
METRICS_BASE_URL: debugMode
? 'https://staging-d.ghostery.com'
: 'https://d.ghostery.com',
EXTENSION_VERSION: version,
Expand Down
26 changes: 0 additions & 26 deletions extension-manifest-v3/src/content_scripts/iframe.js

This file was deleted.

Loading
Loading