Files
OpenFrontIO/src/client/GoogleAdElement.ts
T
Ryan 5e6c90d9bb Main Menu UI Overhaul (#2829)
## Description:

Overhauls the Main Menu UI, visit https://menu.openfront.dev to see
everything.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

w.o.n
2026-01-09 20:26:34 -08:00

98 lines
2.5 KiB
TypeScript

import { LitElement, html } from "lit";
import { customElement, property } from "lit/decorators.js";
declare global {
interface Window {
adsbygoogle: unknown[];
}
}
/**
* Google AdSense integration component
*
* This component creates a configurable container for Google AdSense ads
* and properly initializes them after the component is rendered.
*/
@customElement("google-ad")
export class GoogleAdElement extends LitElement {
// Configurable properties
@property({ type: String }) adClient = "ca-pub-7035513310742290";
@property({ type: String }) adSlot = "5220834834";
@property({ type: String }) adFormat = "auto";
@property({ type: Boolean }) fullWidthResponsive = true;
@property({ type: String }) adTest = "off"; // "on" for testing, remove or set to "off" for production
// Disable shadow DOM so AdSense can access the elements
createRenderRoot() {
return this;
}
render() {
if (isElectron()) {
return html``;
}
return html`
<div class="mt-4 rounded-lg p-2 w-full overflow-hidden">
<ins
class="adsbygoogle block"
data-ad-client="${this.adClient}"
data-ad-slot="${this.adSlot}"
data-ad-format="${this.adFormat}"
data-full-width-responsive="${this.fullWidthResponsive}"
data-adtest="${this.adTest}"
></ins>
</div>
`;
}
connectedCallback() {
super.connectedCallback();
// Wait for the component to be fully rendered
setTimeout(() => {
try {
// Initialize this specific ad
(window.adsbygoogle = window.adsbygoogle || []).push({});
console.log("Ad initialized for slot:", this.adSlot);
} catch (e) {
console.error("AdSense initialization error for slot:", this.adSlot, e);
}
}, 100);
}
}
// Check if running in Electron
const isElectron = () => {
// Renderer process
if (
window !== undefined &&
typeof window.process === "object" &&
// @ts-expect-error hidden
window.process.type === "renderer"
) {
return true;
}
// Main process
if (
process !== undefined &&
typeof process.versions === "object" &&
!!process.versions.electron
) {
return true;
}
// Detect the user agent when the `nodeIntegration` option is set to false
if (
typeof navigator === "object" &&
typeof navigator.userAgent === "string" &&
navigator.userAgent.indexOf("Electron") >= 0
) {
return true;
}
return false;
};
export default GoogleAdElement;