235 lines
No EOL
8.1 KiB
JavaScript
235 lines
No EOL
8.1 KiB
JavaScript
// Adsgram SDK integration for rewarded ads
|
|
// This file provides a bridge between Dart code and the Adsgram JavaScript SDK
|
|
|
|
// Wait for SDK to load if it's not immediately available
|
|
(function() {
|
|
let sdkCheckAttempts = 0;
|
|
const maxCheckAttempts = 50; // Check for up to 5 seconds (50 * 100ms)
|
|
|
|
function checkSDK() {
|
|
if (typeof window.Adsgram !== 'undefined') {
|
|
console.log('Adsgram SDK loaded successfully');
|
|
return;
|
|
}
|
|
|
|
sdkCheckAttempts++;
|
|
if (sdkCheckAttempts < maxCheckAttempts) {
|
|
setTimeout(checkSDK, 100);
|
|
} else {
|
|
console.warn('Adsgram SDK not loaded after timeout. Make sure sad.min.js is included in index.html');
|
|
}
|
|
}
|
|
|
|
// Start checking after a short delay to allow script to load
|
|
setTimeout(checkSDK, 100);
|
|
})();
|
|
|
|
// Store callbacks for ad lifecycle events
|
|
let rewardCallback = null;
|
|
let errorCallback = null;
|
|
let currentAdController = null;
|
|
|
|
// Default block ID (can be overridden)
|
|
const DEFAULT_BLOCK_ID = "16505";
|
|
|
|
/**
|
|
* Initialize Adsgram ad controller with a specific block ID
|
|
* @param {string} blockId - The Adsgram block ID
|
|
* @returns {object} Adsgram ad controller instance
|
|
* @throws {Error} If SDK is not available or initialization fails
|
|
*/
|
|
function initAdController(blockId) {
|
|
if (typeof window.Adsgram === 'undefined') {
|
|
const error = 'Adsgram SDK not available. Make sure sad.min.js is loaded.';
|
|
console.error(error);
|
|
throw new Error(error);
|
|
}
|
|
|
|
if (!blockId || typeof blockId !== 'string' || blockId.trim() === '') {
|
|
const error = 'Invalid block ID provided';
|
|
console.error(error);
|
|
throw new Error(error);
|
|
}
|
|
|
|
try {
|
|
const controller = window.Adsgram.init({ blockId: blockId });
|
|
if (!controller) {
|
|
throw new Error('Failed to initialize Adsgram controller: init returned null');
|
|
}
|
|
return controller;
|
|
} catch (error) {
|
|
const errorMessage = error?.message || error?.toString() || 'Unknown initialization error';
|
|
console.error('Failed to initialize Adsgram controller:', errorMessage);
|
|
throw new Error(`Failed to initialize Adsgram controller: ${errorMessage}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Show a rewarded ad using the default block ID
|
|
* Called from Dart code via js_util.callMethod
|
|
* @returns {Promise} Promise that resolves when ad completes or rejects on error
|
|
*/
|
|
function showAd() {
|
|
return showAdWithBlockId(DEFAULT_BLOCK_ID);
|
|
}
|
|
|
|
/**
|
|
* Show a rewarded ad with a specific block ID
|
|
* Called from Dart code via js_util.callMethod
|
|
* @param {string} blockId - The Adsgram block ID to use
|
|
* @returns {Promise} Promise that resolves when ad completes or rejects on error
|
|
*/
|
|
function showAdWithBlockId(blockId) {
|
|
// Validate block ID
|
|
if (!blockId || typeof blockId !== 'string' || blockId.trim() === '') {
|
|
const error = 'Invalid block ID provided';
|
|
console.error(error);
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
try {
|
|
errorCallback(error);
|
|
} catch (callbackError) {
|
|
console.error('Error in error callback:', callbackError);
|
|
}
|
|
}
|
|
return Promise.reject(new Error(error));
|
|
}
|
|
|
|
// Check SDK availability
|
|
if (typeof window.Adsgram === 'undefined') {
|
|
const error = 'Adsgram SDK not available. Make sure sad.min.js is loaded.';
|
|
console.error(error);
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
try {
|
|
errorCallback(error);
|
|
} catch (callbackError) {
|
|
console.error('Error in error callback:', callbackError);
|
|
}
|
|
}
|
|
return Promise.reject(new Error(error));
|
|
}
|
|
|
|
try {
|
|
// Initialize controller for this specific block
|
|
currentAdController = initAdController(blockId);
|
|
|
|
if (!currentAdController || typeof currentAdController.show !== 'function') {
|
|
const error = 'Invalid ad controller: show method not available';
|
|
console.error(error);
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
try {
|
|
errorCallback(error);
|
|
} catch (callbackError) {
|
|
console.error('Error in error callback:', callbackError);
|
|
}
|
|
}
|
|
return Promise.reject(new Error(error));
|
|
}
|
|
|
|
// Show the ad
|
|
return currentAdController.show().then((result) => {
|
|
// Ad completed successfully - user watched till the end
|
|
console.log('Ad completed successfully', result);
|
|
|
|
// Call reward callback if set
|
|
if (rewardCallback && typeof rewardCallback === 'function') {
|
|
try {
|
|
rewardCallback();
|
|
} catch (callbackError) {
|
|
console.error('Error in reward callback:', callbackError);
|
|
// Don't fail the promise if callback has an error
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}).catch((error) => {
|
|
// Ad failed or was closed early
|
|
console.error('Ad failed or was closed:', error);
|
|
|
|
// Format error message
|
|
let errorMessage = 'Ad failed';
|
|
if (error) {
|
|
if (typeof error === 'object') {
|
|
if (error.message) {
|
|
errorMessage = error.message;
|
|
} else {
|
|
try {
|
|
errorMessage = JSON.stringify(error);
|
|
} catch (e) {
|
|
errorMessage = error.toString();
|
|
}
|
|
}
|
|
} else {
|
|
errorMessage = error.toString();
|
|
}
|
|
}
|
|
|
|
// Call error callback if set
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
try {
|
|
errorCallback(errorMessage);
|
|
} catch (callbackError) {
|
|
console.error('Error in error callback:', callbackError);
|
|
}
|
|
}
|
|
|
|
throw error;
|
|
});
|
|
} catch (error) {
|
|
// Initialization or show() call failed
|
|
let errorMessage = 'Failed to show ad';
|
|
if (error) {
|
|
if (error.message) {
|
|
errorMessage = error.message;
|
|
} else {
|
|
errorMessage = error.toString();
|
|
}
|
|
}
|
|
console.error('Failed to show ad:', errorMessage);
|
|
|
|
if (errorCallback && typeof errorCallback === 'function') {
|
|
try {
|
|
errorCallback(errorMessage);
|
|
} catch (callbackError) {
|
|
console.error('Error in error callback:', callbackError);
|
|
}
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the reward callback function
|
|
* Called from Dart code to register callback for successful ad completion
|
|
* @param {Function} callback - Function to call when ad completes successfully
|
|
*/
|
|
function setRewardCallback(callback) {
|
|
if (callback && typeof callback === 'function') {
|
|
rewardCallback = callback;
|
|
console.log('Reward callback registered');
|
|
} else {
|
|
console.warn('Invalid reward callback provided');
|
|
rewardCallback = null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set the error callback function
|
|
* Called from Dart code to register callback for ad errors
|
|
* @param {Function} callback - Function to call when ad fails (takes error message as parameter)
|
|
*/
|
|
function setErrorCallback(callback) {
|
|
if (callback && typeof callback === 'function') {
|
|
errorCallback = callback;
|
|
console.log('Error callback registered');
|
|
} else {
|
|
console.warn('Invalid error callback provided');
|
|
errorCallback = null;
|
|
}
|
|
}
|
|
|
|
// Make functions available globally for Dart interop
|
|
window.showAd = showAd;
|
|
window.showAdWithBlockId = showAdWithBlockId;
|
|
window.setRewardCallback = setRewardCallback;
|
|
window.setErrorCallback = setErrorCallback; |