Skip to content

Commit

Permalink
Detect embedded and skip addons (#415)
Browse files Browse the repository at this point in the history
Check if addons library is being loaded inside an iframe, and skip
loading it.

Closes #412
  • Loading branch information
humitos authored Nov 18, 2024
1 parent 0b605f6 commit 36220cf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 3 additions & 3 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import * as hotkeys from "./hotkeys";
import * as linkpreviews from "./linkpreviews";
import * as filetreediff from "./filetreediff";
import * as customscript from "./customscript";
import { default as objectPath } from "object-path";
import {
domReady,
isEmbedded,
IS_PRODUCTION,
setupLogging,
getMetadataValue,
Expand Down Expand Up @@ -46,6 +48,15 @@ export function setup() {
return getReadTheDocsConfig(sendUrlParam);
})
.then((config) => {
const loadWhenEmbedded = objectPath.get(
config,
"addons.options.load_when_embedded",
false,
);
if (isEmbedded() && !loadWhenEmbedded) {
return false;
}

const httpStatus = getMetadataValue("readthedocs-http-status");
let promises = [];

Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ export const domReady = new Promise((resolve) => {
}
});

/**
* Check if addons are running inside iframe.
*
* The simplest way to check that is comparing the current windown with the parent window.
* If they are different, it means we are embedded.
*/
export function isEmbedded() {
return window.self !== window.parent;
}

/**
* Addon base class
*
Expand Down

0 comments on commit 36220cf

Please sign in to comment.