Give your grad the gift of
MIT Technology Review
Give a gift
const widget = document.querySelector('.pn-article');
const widgetScope = angular.element(widget).scope();
widgetScope.description = ``;
const targetAnchorsClass = 'a.pn-article__button';
const capturedAngularElementId = 'modal-image-custom-field';
changeAnchorParams(targetAnchorsClass, capturedAngularElementId);
function changeAnchorParams(anchorClass, angularId) {
const domElement = document.getElementById(angularId);
const ngElement = angular.element(domElement);
ngElement.scope().$watchCollection('params', (newParams, oldParams) => {
if (newParams.url === oldParams.url) return;
const userUrl = newParams.url;
const paramsToReplace = (new URL(userUrl)).searchParams;
if (0 === [...paramsToReplace.keys()].length) return;
const anchorsToChange = document.querySelectorAll(anchorClass);
if (0 === anchorsToChange.length) return;
anchorsToChange.forEach((anchor) => {
const anchorHref = new URL(anchor.href);
const anchorSearchParams = anchorHref.searchParams;
[...paramsToReplace.keys()].forEach((param) => {
if (! param.startsWith('utm_') && ! param.startsWith('itm_')) return;
if (anchorSearchParams.has(param)) return;
anchorSearchParams.set(param, paramsToReplace.get(param));
});
const newAnchorHref = anchorHref.toString();
anchor.href = decodeURIComponent(newAnchorHref);
});
});
}