Files

274 lines
6.4 KiB
Plaintext
Raw Permalink Normal View History

2022-03-06 19:52:19 +01:00
import { scheduleOnce } from "@ember/runloop";
import { htmlSafe } from "@ember/template";
2024-11-29 10:11:21 +00:00
import { classNameBindings } from "@ember-decorators/component";
2023-11-07 17:07:33 +00:00
import RSVP from "rsvp";
import discourseComputed from "discourse/lib/decorators";
import { isTesting } from "discourse/lib/environment";
2023-11-07 17:07:33 +00:00
import loadScript from "discourse/lib/load-script";
import { i18n } from "discourse-i18n";
import AdComponent from "./ad-component";
2015-08-25 14:30:23 +10:00
2019-04-16 11:57:16 -04:00
let _loaded = false,
2018-10-22 19:49:32 +01:00
_promise = null,
renderCounts = {};
2015-08-25 14:30:23 +10:00
2017-05-29 18:00:38 -04:00
function parseAdWidth(value) {
2018-10-22 19:49:32 +01:00
if (value === "responsive") {
return "auto";
}
if (value.startsWith("fluid")) {
return "fluid";
}
const w = parseInt(value.substring(0, 3).trim(), 10);
if (isNaN(w)) {
return "auto";
} else {
return `${w}px`;
}
2015-08-25 14:30:23 +10:00
}
2017-05-29 18:00:38 -04:00
function parseAdHeight(value) {
2018-10-22 19:49:32 +01:00
if (value === "responsive") {
return "auto";
}
if (value.startsWith("fluid")) {
return "fluid";
}
const h = parseInt(value.substring(4, 7).trim(), 10);
if (isNaN(h)) {
return "auto";
} else {
return `${h}px`;
}
2015-08-25 14:30:23 +10:00
}
function loadAdsense() {
if (_loaded) {
2022-03-06 19:52:19 +01:00
return RSVP.resolve();
2015-09-02 15:23:17 +10:00
}
2015-09-04 10:51:44 +10:00
if (_promise) {
return _promise;
}
2016-02-22 12:11:29 -05:00
2019-04-16 11:57:16 -04:00
const adsenseSrc =
2018-10-22 19:49:32 +01:00
("https:" === document.location.protocol ? "https:" : "http:") +
"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
2020-09-04 13:24:14 +02:00
_promise = loadScript(adsenseSrc, { scriptTag: true }).then(function () {
_loaded = true;
});
2016-02-22 12:11:29 -05:00
return _promise;
2016-02-22 12:11:29 -05:00
}
2015-08-25 14:30:23 +10:00
const DESKTOP_SETTINGS = {
"topic-list-top": {
code: "adsense_topic_list_top_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_topic_list_top_ad_sizes",
},
"topic-above-post-stream": {
code: "adsense_topic_above_post_stream_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_topic_above_post_stream_ad_sizes",
},
"topic-above-suggested": {
code: "adsense_topic_above_suggested_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_topic_above_suggested_ad_sizes",
},
"post-bottom": {
code: "adsense_post_bottom_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_post_bottom_ad_sizes",
},
};
2015-08-25 14:30:23 +10:00
const MOBILE_SETTINGS = {
"topic-list-top": {
code: "adsense_mobile_topic_list_top_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_mobile_topic_list_top_ad_size",
},
"topic-above-post-stream": {
code: "adsense_mobile_topic_above_post_stream_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_mobile_topic_above_post_stream_ad_size",
},
"topic-above-suggested": {
code: "adsense_mobile_topic_above_suggested_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_mobile_topic_above_suggested_ad_size",
},
"post-bottom": {
code: "adsense_mobile_post_bottom_code",
2020-09-04 13:24:14 +02:00
sizes: "adsense_mobile_post_bottom_ad_size",
},
};
2015-08-25 14:30:23 +10:00
2024-11-29 10:11:21 +00:00
@classNameBindings(
":google-adsense",
"classForSlot",
"isResponsive:adsense-responsive"
)
export default class GoogleAdsense extends AdComponent {
loadedGoogletag = false;
publisher_id = null;
ad_width = null;
ad_height = null;
adRequested = false;
2017-05-29 18:00:38 -04:00
2016-11-08 16:46:09 -05:00
init() {
let config, size;
const placement = this.get("placement");
if (this.site.mobileView) {
config = MOBILE_SETTINGS[placement];
} else {
config = DESKTOP_SETTINGS[placement];
}
if (!renderCounts[placement]) {
renderCounts[placement] = 0;
}
const sizes = (this.siteSettings[config.sizes] || "").split("|");
2019-07-08 15:18:34 -04:00
if (sizes.length === 1) {
size = sizes[0];
} else {
size = sizes[renderCounts[placement] % sizes.length];
renderCounts[placement] += 1;
}
this.set("ad_width", parseAdWidth(size));
this.set("ad_height", parseAdHeight(size));
this.set("ad_code", this.siteSettings[config.code]);
this.set("publisher_id", this.siteSettings.adsense_publisher_code);
2024-11-29 10:11:21 +00:00
super.init();
}
async _triggerAds() {
2022-03-06 19:52:19 +01:00
if (isTesting()) {
return; // Don't load external JS during tests
}
2018-10-22 19:49:32 +01:00
this.set("adRequested", true);
await loadAdsense();
if (this.isDestroyed || this.isDestroying) {
// Component removed from DOM before script loaded
return;
}
try {
const adsbygoogle = (window.adsbygoogle ||= []);
adsbygoogle.push({}); // ask AdSense to fill one ad unit
} catch (ex) {
// eslint-disable-next-line no-console
console.error("Adsense error:", ex);
}
2024-11-29 10:11:21 +00:00
}
2016-11-08 16:46:09 -05:00
didInsertElement() {
2024-11-29 10:11:21 +00:00
super.didInsertElement();
2018-10-22 19:49:32 +01:00
if (!this.get("showAd")) {
return;
}
2022-03-06 19:52:19 +01:00
scheduleOnce("afterRender", this, this._triggerAds);
2024-11-29 10:11:21 +00:00
}
2016-11-08 16:46:09 -05:00
@discourseComputed("ad_width")
2019-04-18 14:10:56 -04:00
isResponsive(adWidth) {
return ["auto", "fluid"].includes(adWidth);
2024-11-29 10:11:21 +00:00
}
@discourseComputed("ad_width")
isFluid(adWidth) {
return adWidth === "fluid";
2024-11-29 10:11:21 +00:00
}
2017-05-29 18:00:38 -04:00
@discourseComputed("placement", "showAd")
2019-04-18 14:10:56 -04:00
classForSlot(placement, showAd) {
return showAd ? htmlSafe(`adsense-${placement}`) : "";
2024-11-29 10:11:21 +00:00
}
2017-05-29 18:00:38 -04:00
@discourseComputed("isResponsive", "isFluid")
autoAdFormat(isResponsive, isFluid) {
return isResponsive ? htmlSafe(isFluid ? "fluid" : "auto") : false;
2024-11-29 10:11:21 +00:00
}
2017-05-29 18:00:38 -04:00
@discourseComputed("ad_width", "ad_height", "isResponsive")
2019-04-18 14:10:56 -04:00
adWrapperStyle(w, h, isResponsive) {
return htmlSafe(isResponsive ? "" : `width: ${w}; height: ${h};`);
2024-11-29 10:11:21 +00:00
}
2015-08-25 14:30:23 +10:00
@discourseComputed("adWrapperStyle", "isResponsive")
2019-04-18 14:10:56 -04:00
adInsStyle(adWrapperStyle, isResponsive) {
return htmlSafe(
`display: ${isResponsive ? "block" : "inline-block"}; ${adWrapperStyle}`
);
2024-11-29 10:11:21 +00:00
}
2015-08-25 14:30:23 +10:00
2024-01-31 11:04:24 -07:00
@discourseComputed
showAdsenseAds() {
2024-01-31 11:04:24 -07:00
if (!this.currentUser) {
return true;
}
return this.currentUser.show_adsense_ads;
2024-11-29 10:11:21 +00:00
}
@discourseComputed(
"publisher_id",
"showAdsenseAds",
"showToGroups",
"showAfterPost",
"showOnCurrentPage"
)
showAd(
publisherId,
showAdsenseAds,
showToGroups,
showAfterPost,
showOnCurrentPage
) {
2019-04-18 14:10:56 -04:00
return (
publisherId &&
showAdsenseAds &&
2019-04-18 14:10:56 -04:00
showToGroups &&
showAfterPost &&
showOnCurrentPage
2019-04-18 14:10:56 -04:00
);
2024-11-29 10:11:21 +00:00
}
2019-04-18 17:52:59 -04:00
@discourseComputed("postNumber")
2019-04-18 17:52:59 -04:00
showAfterPost(postNumber) {
if (!postNumber) {
return true;
}
return this.isNthPost(
parseInt(this.siteSettings.adsense_nth_post_code, 10)
);
2024-11-29 10:11:21 +00:00
}
<template>
{{#if this.showAd}}
<div class="google-adsense-label"><h2>{{i18n
"adplugin.advertisement_label"
}}</h2></div>
<div
class="google-adsense-content"
id={{if this.isResponsive "google-adsense__responsive"}}
style={{this.adWrapperStyle}}
>
<ins
class="adsbygoogle"
style={{this.adInsStyle}}
data-ad-client="ca-pub-{{this.publisher_id}}"
data-ad-slot={{this.ad_code}}
data-ad-format={{this.autoAdFormat}}
>
</ins>
</div>
{{/if}}
</template>
2024-11-29 10:11:21 +00:00
}