Skip to content

Commit

Permalink
AG-37031 Fix link breakage due to deletion of amp string. #102
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 00428fd
Author: jellizaveta <[email protected]>
Date:   Thu Oct 24 13:39:42 2024 +0300

    fix issue with selectors, remove redundant conditions

commit a04db2a
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 19:28:06 2024 +0300

    clean added links too

commit 7aad8cc
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 18:14:01 2024 +0300

    update changelog

commit 84a2333
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 18:05:01 2024 +0300

    fix vars, update config

commit bda23bf
Author: Slava Leleka <[email protected]>
Date:   Wed Oct 23 17:01:15 2024 +0300

    CHANGELOG.md edited online with Bitbucket

commit 56c2a9b
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 16:23:49 2024 +0300

    remove watch from webpack config

commit a1a5d36
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 16:20:56 2024 +0300

    remove redundant func

commit 2c6e95d
Author: jellizaveta <[email protected]>
Date:   Wed Oct 23 16:15:28 2024 +0300

    remove replacement/deletion of 'amp' string in links, added watch mode for debugging, rewrite the code for redirects

commit 395d5b0
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 18:27:04 2024 +0300

    update meta

commit 27450a7
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 18:22:34 2024 +0300

    update changes

commit 29f0a1b
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 18:21:38 2024 +0300

    limit fix

commit 4ccec6d
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 18:21:22 2024 +0300

    limit fix

commit a4e9314
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 22 16:17:45 2024 +0300

    CHANGELOG.md edited online with Bitbucket

commit 70c3d83
Author: Slava Leleka <[email protected]>
Date:   Tue Oct 22 16:04:39 2024 +0300

    src/google-amp.js edited online with Bitbucket

commit 8d2c257
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 15:59:51 2024 +0300

    update changelog

commit 8b57d25
Author: jellizaveta <[email protected]>
Date:   Tue Oct 22 15:57:36 2024 +0300

    AG-37031 handle cases where amp should be replaced by HTML file extension. #102
  • Loading branch information
jellizaveta committed Oct 24, 2024
1 parent 59f05d4 commit 7d17631
Show file tree
Hide file tree
Showing 8 changed files with 1,569 additions and 2,301 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
[Keep a Changelog]: https://keepachangelog.com/en/1.0.0/
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html

## [1.0.51] - 2024-10-23

### Added

- `cleanAmpLink` function, that forces to open google news link in new tab, necessary for proper redirection. [#102].

### Fixed

- Link breakage due to deletion of amp string. [#102].

[1.0.51]: https://github.com/AdguardTeam/Scriptlets/compare/v1.0.49...v1.0.51
[#102]: https://github.com/AdguardTeam/DisableAMP/issues/102

## [1.0.49] - 2024-10-18

### Added
Expand Down
3 changes: 2 additions & 1 deletion meta.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
// @include https://*/?amp*
// @include https://*/*&amp*
// @include https://*/*&amp=1*
// @include https://*?amp=1*
// @include https://*?amp=*
// @include https://*/*?amp=1*
// @include https://*/amp-*/*
// @include https://*/ampNews/*
// @exclude
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"dev": "NODE_ENV=DEV ./node_modules/.bin/webpack",
"beta": "NODE_ENV=BETA ./node_modules/.bin/webpack",
"release": "NODE_ENV=RELEASE ./node_modules/.bin/webpack",
"watch": "NODE_ENV=DEV ./node_modules/.bin/webpack --watch",
"lint": "eslint ./src",
"locales:download": "LOCALES=DOWNLOAD node locales.js",
"locales:upload": "LOCALES=UPLOAD node locales.js",
Expand Down Expand Up @@ -38,7 +39,7 @@
"html-webpack-plugin": "^3.2.0",
"husky": "^4.3.0",
"replace-in-file": "^4.1.1",
"webpack": "^4.35.3",
"webpack": "^5.95.0",
"webpack-cli": "^3.3.5"
}
}
14 changes: 0 additions & 14 deletions src/amp-project.js

This file was deleted.

90 changes: 35 additions & 55 deletions src/google-amp.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,56 @@
import {
sanitizeUrl,
hideAmpIcon,
sanitizeLinkElement,
extractCanonicalFromJslog,
} from './utils';
import { hideAmpIcon, sanitizeLinkElement, extractCanonicalFromJslog } from './utils';

const DISABLE_AMP_REDIRECTED = '__disable_amp_redirected';
const URL_PATTERN_REGEX = /^https?:\/\/.+/i;
const ARTICLE_VIEW_AMP = 'articleViewAmp';
const expando = `__${Math.random()}`;
const HTTPS = 'https://';
const AMP_ATTRIBUTES_TO_REMOVE = [
'ping',
'data-ved',
'data-amp-cur',
'data-amp-title',
'data-amp',
'data-amp-vgi',
];

/**
* Redirects amp version to normal
* Prevent amp links from open in google iframe
* e.g. google.com/amp/amp.website.com
*/
export const ampRedirect = () => {
// timeout to prevent automatic redirects to amp
const REDIRECT_WAIT_TIME_OUT_MS = 30000;
const disableAmpRedirectedDate = Number(sessionStorage.getItem(DISABLE_AMP_REDIRECTED));
if (
// Prevent redirecting to another page if run in an iframe
window.self !== window.top
// Do not redirect if since last redirect past less than REDIRECT_WAIT_TIME_OUT_MS
|| (disableAmpRedirectedDate
&& Date.now() - disableAmpRedirectedDate < REDIRECT_WAIT_TIME_OUT_MS)
) {
return;
}
const canonicalLink = document.querySelector('head > link[rel="canonical"]');
const ampProjectScript = document.querySelector('head > script[src^="https://cdn.ampproject.org"]');
if (ampProjectScript && canonicalLink && URL_PATTERN_REGEX.test(canonicalLink.href)) {
sessionStorage.setItem(DISABLE_AMP_REDIRECTED, Date.now());
window.top.location.href = canonicalLink.href;
}
export const cleanAmpLink = () => {
const ampLinks = document.querySelectorAll('a[data-amp]');
ampLinks.forEach((link) => {
AMP_ATTRIBUTES_TO_REMOVE.forEach((attr) => {
link.removeAttribute(attr);
});
});
};

/**
* Replaces amp links by data-amp-cur attribute value
* Redirects amp version to normal
*/
const replaceByAmpCurAttribute = () => {
const elements = document.querySelectorAll('a[data-amp-cur]');
[...elements].forEach((el) => {
if (el[expando]) {
return;
}
export const ampRedirect = () => {
const canonicalLink = document.querySelector('head > link[rel="canonical"]');
if (!canonicalLink) {
return;
}

// eslint-disable-next-line no-param-reassign
el[expando] = true;
if (!URL_PATTERN_REGEX.test(canonicalLink.href)) {
return;
}

const url = el.getAttribute('data-amp-cur') || el.getAttribute('data-amp');
if (url) {
sanitizeLinkElement(el, sanitizeUrl(url));
}
// iframe do not have this marker
const ampMarker = document.querySelector('script[src^="https://cdn.ampproject.org/"]');
if (!ampMarker) {
return;
}

// Some websites manage to break the data-amp-cur attribute and use articleViewAmp htmls instead,
// e.g http://www.mediatoday.co.kr/news/articleViewAmp.html?idxno=313292
const { href } = el;
if (href && href.includes(ARTICLE_VIEW_AMP)) {
sanitizeLinkElement(el, href.replace(ARTICLE_VIEW_AMP, 'articleView'));
}
});
// redirect to canonical link if current page is not iframe
document.location.href = canonicalLink.href;
};

/**
* Replaces amp links provided by amp cdn
*/
const replaceCdnAmp = () => {
export const replaceCdnAmp = () => {
const ampLinks = document.querySelectorAll('a[data-amp-cdn]');
ampLinks.forEach((ampLink) => {
let fixedUrl = ampLink.href;
Expand Down Expand Up @@ -93,11 +78,6 @@ const replaceCdnAmp = () => {
});
};

export const preventAmp = () => {
replaceByAmpCurAttribute();
replaceCdnAmp();
};

export const preventNewsAmp = () => {
if (window.self !== window.top) {
return;
Expand Down
30 changes: 22 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
import { redirectTurboPages } from './yandex-turbo';
import { redirectAmpProjectPages } from './amp-project';
import { preventNewsAmp, preventAmp, ampRedirect } from './google-amp';
import {
preventNewsAmp,
replaceCdnAmp,
ampRedirect,
cleanAmpLink,
} from './google-amp';
import { observeDomChanges } from './utils';

const SEARCH_PATH = '/search';
const GOOGLE_NEWS_PATH = 'news.google.';
const GOOGLE_PATH = 'google.';
const YANDEX_TURBO_PATH = 'https://yandex.ru/turbo';
const TURBOPAGES_PATH = 'turbopages.org';

(() => {
const { href, origin } = document.location;

if (href.includes('https://yandex.ru/turbo') || href.includes('turbopages.org')) {
if (href.includes(YANDEX_TURBO_PATH) || href.includes(TURBOPAGES_PATH)) {
redirectTurboPages();
return;
}

if (!origin.includes('.google.')) {
// clean links in google search result to prevent them from open in iframe
if (origin.includes(GOOGLE_PATH) && href.includes(SEARCH_PATH)) {
observeDomChanges(cleanAmpLink);
}

// redirect only amp links
if (!origin.includes(GOOGLE_PATH)) {
ampRedirect();
redirectAmpProjectPages();
return;
}

if (origin.includes('news.google.')) {
if (origin.includes(GOOGLE_NEWS_PATH)) {
observeDomChanges(preventNewsAmp);
} else {
observeDomChanges(preventAmp);
observeDomChanges(replaceCdnAmp);
}
})();
8 changes: 0 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ export const sanitizeLinkElement = (el, url) => {
hideAmpIcon(el);
};

/**
* Removes amp marker from url
*
* @param {string} ampUrl amp url
* @returns {string} url without amp marker
*/
export const sanitizeUrl = (ampUrl) => ampUrl.replace(AMP_TOKEN_REGEXP, '');

/**
* Extracts canonical url from link tags which use jslog attribute to store
* encoded data. Encoded data may store only canonical url or both canonical
Expand Down
Loading

0 comments on commit 7d17631

Please sign in to comment.