Skip to content
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
34 changes: 22 additions & 12 deletions src/ethicalads.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,23 @@ export class EthicalAdsAddon extends AddonBase {
if (elementToAppend) {
elementToAppend.append(placement);
}
} else if (window.innerWidth > 1300) {
// https://ethical-ad-client.readthedocs.io/en/latest/#stickybox
placement.setAttribute("data-ea-type", "image");
placement.setAttribute("data-ea-style", "stickybox");
this.addEaPlacementToElement(placement);
// `document.body` here is not too much relevant, since we are going to
// use this selector only for a floating stickybox ad
const elementInsertBefore = document.body;
elementInsertBefore.insertBefore(
placement,
elementInsertBefore.lastChild,
);
} else {
// Default to a text ad appended to the root selector when no known placement found
placement.setAttribute("data-ea-type", "text");
// TODO: Check this placement on the dashboard,
// and see how this is performing.
const docToolName = docTool.getDocumentationTool();
const idSuffix = docToolName ? `-${docToolName}` : "";
placement.setAttribute("id", `readthedocs-ea-text-footer${idSuffix}`);

const rootSelector = docTool.getRootSelector();
const rootElement = document.querySelector(rootSelector);

if (rootElement) {
rootElement.append(placement);
} else {
console.debug("Could not find root element to append ad");
}
}
}

Expand Down Expand Up @@ -239,6 +244,11 @@ export class EthicalAdsAddon extends AddonBase {
}

elementAboveTheFold(element) {
// Return false if element doesn't exist
if (!element) {
return false;
}

// Determine if this element would be above the fold.
// If this is off screen, instead create an ad in the footer.
// Assumes the ad would be AD_SIZE pixels high.
Expand Down
5 changes: 2 additions & 3 deletions tests/__snapshots__/ethicalads.test.snap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ snapshots["EthicalAd addon ad placement injected"] =
data-ea-keywords="docs|data-science"
data-ea-manual="true"
data-ea-publisher="readthedocs"
data-ea-style="stickybox"
data-ea-type="image"
id="readthedocs-ea"
data-ea-type="text"
id="readthedocs-ea-text-footer"
>
</div>
`;
Expand Down
14 changes: 10 additions & 4 deletions tests/ethicalads.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,17 @@
});

it("ad placement injected", async () => {
// We need the width to be bigger than 1300px for the ad to be injected
await setViewport({ width: 1600, height: 640 });

const addon = new ethicalads.EthicalAdsAddon(config);
const element = document.querySelector("#readthedocs-ea");
const element = document.querySelector(
"#readthedocs-ea-text-footer",
);

expect(element).to.exist;
expect(element).to.have.attribute("data-ea-type", "text");
expect(element).to.have.attribute(
"data-ea-publisher",
"readthedocs",
);
await expect(element).dom.to.equalSnapshot();
});

Expand Down