Skip to content

Commit

Permalink
Updated refererInfo logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardi committed May 17, 2022
1 parent 6e28596 commit b31e7cd
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 99 deletions.
62 changes: 47 additions & 15 deletions src/refererDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,28 @@
*/

import { config } from './config.js';
import { logWarn } from './utils.js';
import {logWarn} from './utils.js';

/**
* Convert urls of the form "example.com" or "//example.com" to "http(s)://example.com"
*/
export function ensureProtocol(url, win = window) {
if (!url) return url;
if (/\w+:\/\//.exec(url)) {
// url already has protocol
return url;
}
let windowProto = win.location.protocol;
try {
windowProto = win.top.location.protocol;
} catch (e) {}
if (/^\/\//.exec(url)) {
// url uses relative protocol ("//example.com")
return windowProto + url;
} else {
return `${windowProto}//${url}`;
}
}

/**
* @param {Window} win Window
Expand Down Expand Up @@ -79,7 +100,7 @@ export function detectReferer(win) {
const ancestors = getAncestorOrigins(win);
const maxNestedIframes = config.getConfig('maxNestedIframes');
let currentWindow;
let bestReferrer;
let bestLocation;
let bestCanonicalUrl;
let reachedTop = false;
let level = 0;
Expand All @@ -91,7 +112,7 @@ export function detectReferer(win) {
const wasInAmpFrame = inAmpFrame;
let currentLocation;
let crossOrigin = false;
let foundReferrer = null;
let foundLocation = null;

inAmpFrame = false;
currentWindow = currentWindow ? currentWindow.parent : win;
Expand All @@ -107,8 +128,8 @@ export function detectReferer(win) {
const context = previousWindow.context;

try {
foundReferrer = context.sourceUrl;
bestReferrer = foundReferrer;
foundLocation = context.sourceUrl;
bestLocation = foundLocation;

valuesFromAmp = true;

Expand All @@ -124,29 +145,30 @@ export function detectReferer(win) {
logWarn('Trying to access cross domain iframe. Continuing without referrer and location');

try {
// the referrer to an iframe is the parent window
const referrer = previousWindow.document.referrer;

if (referrer) {
foundReferrer = referrer;
foundLocation = referrer;

if (currentWindow === win.top) {
reachedTop = true;
}
}
} catch (e) { /* Do nothing */ }

if (!foundReferrer && ancestors && ancestors[level - 1]) {
foundReferrer = ancestors[level - 1];
if (!foundLocation && ancestors && ancestors[level - 1]) {
foundLocation = ancestors[level - 1];
}

if (foundReferrer && !valuesFromAmp) {
bestReferrer = foundReferrer;
if (foundLocation && !valuesFromAmp) {
bestLocation = foundLocation;
}
}
} else {
if (currentLocation) {
foundReferrer = currentLocation;
bestReferrer = foundReferrer;
foundLocation = currentLocation;
bestLocation = foundLocation;
valuesFromAmp = false;

if (currentWindow === win.top) {
Expand All @@ -165,19 +187,29 @@ export function detectReferer(win) {
}
}

stack.push(foundReferrer);
stack.push(foundLocation);
level++;
} while (currentWindow !== win.top && level < maxNestedIframes);

stack.reverse();

let topLocation, ref;
try {
topLocation = win.top.location.href;
ref = win.top.document.referrer;
} catch (e) {}

const page = ensureProtocol(bestCanonicalUrl, win) || topLocation || bestLocation || null;

return {
referer: bestReferrer || null,
reachedTop,
isAmp: valuesFromAmp,
numIframes: level - 1,
stack,
canonicalUrl: bestCanonicalUrl || null
location: bestLocation || null, // our best guess at page location - the topmost reachable frame URL
canonicalUrl: bestCanonicalUrl || null, // canonical URL as provided with setConfig({pageUrl}) or link[rel="canonical"], in that order of priority
page: page, // canonicalUrl, falling back to location
ref: ref || null, // window.top.document.referrer, if available
};
}

Expand Down
Loading

0 comments on commit b31e7cd

Please sign in to comment.