This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 975
Top500 ad insertions #46
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,126 @@ ipc.on('zoom-reset', function () { | |
browserZoomLevel = 0 | ||
webFrame.setZoomLevel(browserZoomLevel) | ||
}) | ||
|
||
/** | ||
* Ensures a node replacement div is visible and has a proper zIndex | ||
*/ | ||
function ensureNodeVisible (node) { | ||
if (document.defaultView.getComputedStyle(node).display === 'none') { | ||
node.style.display = '' | ||
} | ||
if (document.defaultView.getComputedStyle(node).zIndex === '-1') { | ||
node.style.zIndex = '' | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What will this do to hidden ad divs that some less scrupulous pubs use? We would want to block those and not replace them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only used on the empty left over nodes after blocking is done. So those should already all be blocked. |
||
} | ||
|
||
/** | ||
* Determines the ad size which should be shown | ||
* It will first check the node's size and try to infer that way. | ||
* If that is not possible it will rely on the iframeData | ||
* | ||
* @param node The node that is being replaced | ||
* @param iframeData The known preprocessed iframeData for that node | ||
*/ | ||
function getAdSize (node, iframeData) { | ||
var acceptableAdSizes = [ | ||
[728, 90], | ||
[300, 250], | ||
[160, 600], | ||
[320, 50] | ||
] | ||
for (var i = 0; i < acceptableAdSizes.length; i++) { | ||
var adSize = acceptableAdSizes[i] | ||
if (node.offsetWidth === adSize[0] && node.offsetHeight >= adSize[1] || | ||
node.offsetWidth >= adSize[0] && node.offsetHeight === adSize[1]) { | ||
return adSize | ||
} | ||
} | ||
|
||
if (iframeData) { | ||
return [iframeData.width, iframeData.height] | ||
} | ||
|
||
return null | ||
} | ||
|
||
/** | ||
* Processes a single node which is an ad | ||
* | ||
* @param node The node of the ad to process | ||
* @param iframeData The iframe data of the node to process from the slimerJS bot | ||
* @param placeholderUrl The vault URL with encoded user ID and session ID to use | ||
*/ | ||
function processAdNode (node, iframeData, placeholderUrl) { | ||
if (!node) { | ||
return | ||
} | ||
|
||
var adSize = getAdSize(node, iframeData) | ||
// Could not determine the ad size, so just skip this replacement | ||
if (!adSize) { | ||
return | ||
} | ||
var srcUrl = placeholderUrl + '&width=' + encodeURIComponent(adSize[0]) + '&height=' + encodeURIComponent(adSize[1]) | ||
if (node.tagName === 'IFRAME') { | ||
node.src = srcUrl | ||
} else { | ||
while (node.firstChild) { | ||
node.removeChild(node.firstChild) | ||
} | ||
var iframe = document.createElement('iframe') | ||
iframe.style.padding = 0 | ||
iframe.style.border = 0 | ||
iframe.style.margin = 0 | ||
iframe.style.width = adSize[0] + 'px' | ||
iframe.style.height = adSize[1] + 'px' | ||
iframe.src = srcUrl | ||
node.appendChild(iframe) | ||
ensureNodeVisible(node) | ||
if (node.parentNode) { | ||
ensureNodeVisible(node.parentNode) | ||
if (node.parentNode) { | ||
ensureNodeVisible(node.parentNode.parentNode) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Fires when the browser has ad replacement information to give | ||
ipc.on('set-ad-div-candidates', function (e, adDivCandidates, placeholderUrl) { | ||
// Keep a lookup for skipped common elements | ||
var fallbackNodeDataForCommon = {} | ||
|
||
// Process all of the specific ad information for this page | ||
adDivCandidates.forEach(function (iframeData) { | ||
var selector = '[id="' + iframeData.replaceId + '"]' | ||
var node = document.querySelector(selector) | ||
if (!node) { | ||
return | ||
} | ||
|
||
// Skip over known common elements | ||
if (iframeData.replaceId.startsWith('google_ads_iframe_') || | ||
iframeData.replaceId.endsWith('__container__')) { | ||
fallbackNodeDataForCommon[node.id] = iframeData | ||
return | ||
} | ||
|
||
// Find the node and process it | ||
processAdNode(document.querySelector(selector), iframeData, placeholderUrl) | ||
}) | ||
|
||
// Common selectors which could be on every page | ||
var commonSelectors = [ | ||
'[id^="google_ads_iframe_"][id$="__container__"]' | ||
] | ||
commonSelectors.forEach(commonSelector => { | ||
var nodes = document.querySelectorAll(commonSelector) | ||
if (!nodes) { | ||
return | ||
} | ||
Array.from(nodes).forEach(node => { | ||
processAdNode(node, fallbackNodeDataForCommon[node.id], placeholderUrl) | ||
}) | ||
}) | ||
}) |
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
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.
We probably want to differentiate between visible on the page and visible in the browser viewport. We don't need the former yet, but we will want it before we start displaying real ads. I put a task in the backlog in asana for it.
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.
Good point.