-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: rendering the winning ad #53
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df672d0
feat: initial setup for rendering the winning ad
7912454
feat: add main export
40ee494
feat: initial function and tests
2b012db
chore: add debug mode
bf0b4c5
fix: add in check to ensure ad is rendered on same origin as auction
95d374f
chore: fix tests
e149897
fix: prevent iframe scrolling
a018d62
chore: add some package keywords
19e7e4f
fix: filter null bids, add more data for auction
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import runAdAuction from './auction/index.js'; | ||
import joinAdInterestGroup from './interest-groups/join.js'; | ||
import leaveAdInterestGroup from './interest-groups/leave.js'; | ||
import renderAd from './render/index.js'; | ||
|
||
export default { | ||
runAdAuction, | ||
joinAdInterestGroup, | ||
leaveAdInterestGroup, | ||
renderAd, | ||
runAdAuction, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Render | ||
|
||
## Install | ||
|
||
```bash | ||
$ npm install @magnite/fledge.polyfill | ||
``` | ||
|
||
## Usage | ||
|
||
```html | ||
<script type="module"> | ||
import { fledge } from "./node_modules/@magnite/fledge.polyfill/esm/index.js"; | ||
|
||
// ...run the auction; see auctions docs for full example | ||
const auctionResults = await fledge.runAdAuction(options); | ||
|
||
fledge.renderAd(id, auctionResults); | ||
</script> | ||
``` | ||
|
||
## API | ||
|
||
### renderAd(id, results) | ||
|
||
Returns `null` if no winning bid is found. Returns a `Promise` with a token representation of the winning bids rendering URL. | ||
|
||
If an invalid option is passed, then an `Error` will be thrown with a reason to help debug. | ||
|
||
#### id | ||
|
||
Type: [`<HTML ID>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id) | ||
|
||
The `id` of the element that will be the target for which the ad will render. | ||
|
||
#### results | ||
|
||
Type: [`<String>`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | ||
|
||
A token, or string, that represents the results from an auction run via the `fledge.runAdAuction` call. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import echo from '../utils/console.js'; | ||
import db, { AUCTION_STORE } from '../utils/db.js'; | ||
import validate from '../utils/validation.js'; | ||
import { | ||
getTarget, | ||
renderFrame, | ||
} from './utils.js'; | ||
|
||
/* | ||
* @function | ||
* @name renderAd | ||
* @description render an ad | ||
* @author Newton <[email protected]> | ||
* @param {string} selector - a string reprensenting a valid selector to find an element on the page | ||
* @param {string} token - a string that represents the results from an auction run via the `fledge.runAdAuction` call | ||
* @throws {Error} Any parameters passed are incorrect or an incorrect type | ||
* @return {Promise<null | true>} | ||
* | ||
* @example | ||
* renderAd('#ad-slot-1', '76941e71-2ed7-416d-9c55-36d07beff786'); | ||
*/ | ||
export default async function renderAd (selector, token, debug = false) { | ||
debug && echo.group('Fledge: Render an Ad'); | ||
debug && echo.log('ad render selector:', selector); | ||
debug && echo.log('ad render token:', token); | ||
validate.param(selector, 'string'); | ||
validate.param(token, 'string'); | ||
|
||
debug && echo.info('checking that target exists on the page'); | ||
const target = getTarget(selector); | ||
debug && echo.log('target:', target); | ||
if (!target) { | ||
throw new Error(`Target not found on the page! Please check that ${target} exists on the page.`); | ||
} | ||
|
||
debug && echo.info('checking that winning token exists'); | ||
const winner = await db.store.get(AUCTION_STORE, token); | ||
debug && echo.log('winners token:', winner); | ||
if (!(winner && winner.id === token)) { | ||
throw new Error(`A token was not found! Token provided: ${token}`); | ||
} | ||
|
||
debug && echo.info('checking that winner to be rendered is on the same hostname as the auction'); | ||
if (winner?.origin !== `${window.top.location.origin}${window.top.location.pathname}`) { | ||
debug && echo.error(`Attempting to render the winner on a location that doesn't match the auctions hostname`, { winner: winner.origin, auction: `${window.top.location.origin}${window.top.location.pathname}` }); | ||
throw new Error('Something went wrong! No ad was rendered.'); | ||
} | ||
debug && echo.info('rendering an iframe with the winning ad'); | ||
renderFrame(target, winner); | ||
|
||
debug && echo.info('checking that ad iframe actually rendered'); | ||
const ad = getTarget(`#fledge-auction-${token}`); | ||
debug && echo.log('ads target:', ad); | ||
if (!ad) { | ||
throw new Error('Something went wrong! No ad was rendered.'); | ||
} | ||
debug && echo.groupEnd(); | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* @function | ||
* @name getTarget | ||
* @description grab a DOM node based on a CSS style selector passed in | ||
* @author Newton <[email protected]> | ||
* @param {string} selector - a CSS style selector | ||
* @throws {Error} if no target is found based on the selector provided | ||
* @return {DOM Node} a DOM node found on the page | ||
*/ | ||
export const getTarget = selector => document.querySelector(selector); | ||
|
||
/* | ||
* @function | ||
* @name renderFrame | ||
* @description renders an iFrame when given a target and source URL | ||
* @author Newton <[email protected]> | ||
* @param {DOM Node} target - a valid DOM node with which to append an iframe | ||
* @param {object} source - a valid winning ad object to render within the iframe | ||
* @throws {Error} if no source URL is found based on the selector provided | ||
* @return {void} | ||
*/ | ||
export const renderFrame = (target, source) => { | ||
if (!source.bid?.render) { | ||
throw new Error(`Something went wrong! No rendering URL was found.`); | ||
} | ||
|
||
const iframe = document.createElement('iframe'); | ||
iframe.id = `fledge-auction-${source.id}`; | ||
iframe.src = source.bid?.render; | ||
iframe.scrolling = 'no'; | ||
iframe.style.borderWidth = 0; | ||
target.appendChild(iframe); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what size is the iframe here? we might need to set the dimensions.