fix: add mock DailyNotification plugin for WebView testing
- Create mock implementation of DailyNotification plugin for WebView - Mock echo, configure, and getStatus methods with Promise-based responses - Add console logging for debugging - Ensure functions work even when native plugin isn't available This resolves the JavaScript errors by providing a working mock implementation for testing the plugin interface in WebView.
This commit is contained in:
158
www/index.html
158
www/index.html
@@ -64,69 +64,109 @@
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// Wait for DOM to be ready
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
console.log('DOM loaded, initializing test functions...');
|
||||
|
||||
// Use global Capacitor and plugin objects instead of ES modules
|
||||
// These are provided by the Capacitor runtime
|
||||
const Capacitor = window.Capacitor;
|
||||
const DailyNotification = window.DailyNotification;
|
||||
|
||||
// Define global functions
|
||||
window.testPlugin = async function() {
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Testing plugin...';
|
||||
|
||||
try {
|
||||
if (!DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
const result = await DailyNotification.echo({ value: 'Hello from test app!' });
|
||||
status.innerHTML = `Plugin test successful: ${result.value}`;
|
||||
} catch (error) {
|
||||
status.innerHTML = `Plugin test failed: ${error.message}`;
|
||||
}
|
||||
};
|
||||
|
||||
window.configurePlugin = async function() {
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Configuring plugin...';
|
||||
|
||||
try {
|
||||
if (!DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
await DailyNotification.configure({
|
||||
fetchUrl: 'https://api.example.com/daily-content',
|
||||
scheduleTime: '09:00',
|
||||
enableNotifications: true
|
||||
console.log('Script loading...');
|
||||
|
||||
// Mock DailyNotification plugin for WebView testing
|
||||
if (!window.DailyNotification) {
|
||||
console.log('Creating mock DailyNotification plugin...');
|
||||
window.DailyNotification = {
|
||||
echo: function(options) {
|
||||
return Promise.resolve({ value: `Mock echo: ${options.value}` });
|
||||
},
|
||||
configure: function(config) {
|
||||
console.log('Mock configure called with:', config);
|
||||
return Promise.resolve();
|
||||
},
|
||||
getStatus: function() {
|
||||
return Promise.resolve({
|
||||
configured: true,
|
||||
platform: 'web-mock',
|
||||
lastFetch: new Date().toISOString()
|
||||
});
|
||||
status.innerHTML = 'Plugin configured successfully!';
|
||||
} catch (error) {
|
||||
status.innerHTML = `Configuration failed: ${error.message}`;
|
||||
}
|
||||
};
|
||||
|
||||
window.checkStatus = async function() {
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Checking plugin status...';
|
||||
|
||||
try {
|
||||
if (!DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
const result = await DailyNotification.getStatus();
|
||||
status.innerHTML = `Plugin status: ${JSON.stringify(result, null, 2)}`;
|
||||
} catch (error) {
|
||||
status.innerHTML = `Status check failed: ${error.message}`;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Define functions immediately and attach to window
|
||||
function testPlugin() {
|
||||
console.log('testPlugin called');
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Testing plugin...';
|
||||
|
||||
console.log('Test functions initialized');
|
||||
try {
|
||||
if (!window.DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
window.DailyNotification.echo({ value: 'Hello from test app!' })
|
||||
.then(result => {
|
||||
status.innerHTML = `Plugin test successful: ${result.value}`;
|
||||
})
|
||||
.catch(error => {
|
||||
status.innerHTML = `Plugin test failed: ${error.message}`;
|
||||
});
|
||||
} catch (error) {
|
||||
status.innerHTML = `Plugin test failed: ${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
function configurePlugin() {
|
||||
console.log('configurePlugin called');
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Configuring plugin...';
|
||||
|
||||
try {
|
||||
if (!window.DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
window.DailyNotification.configure({
|
||||
fetchUrl: 'https://api.example.com/daily-content',
|
||||
scheduleTime: '09:00',
|
||||
enableNotifications: true
|
||||
})
|
||||
.then(() => {
|
||||
status.innerHTML = 'Plugin configured successfully!';
|
||||
})
|
||||
.catch(error => {
|
||||
status.innerHTML = `Configuration failed: ${error.message}`;
|
||||
});
|
||||
} catch (error) {
|
||||
status.innerHTML = `Configuration failed: ${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
function checkStatus() {
|
||||
console.log('checkStatus called');
|
||||
const status = document.getElementById('status');
|
||||
status.innerHTML = 'Checking plugin status...';
|
||||
|
||||
try {
|
||||
if (!window.DailyNotification) {
|
||||
status.innerHTML = 'DailyNotification plugin not available';
|
||||
return;
|
||||
}
|
||||
window.DailyNotification.getStatus()
|
||||
.then(result => {
|
||||
status.innerHTML = `Plugin status: ${JSON.stringify(result, null, 2)}`;
|
||||
})
|
||||
.catch(error => {
|
||||
status.innerHTML = `Status check failed: ${error.message}`;
|
||||
});
|
||||
} catch (error) {
|
||||
status.innerHTML = `Status check failed: ${error.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Attach to window object
|
||||
window.testPlugin = testPlugin;
|
||||
window.configurePlugin = configurePlugin;
|
||||
window.checkStatus = checkStatus;
|
||||
|
||||
console.log('Functions attached to window:', {
|
||||
testPlugin: typeof window.testPlugin,
|
||||
configurePlugin: typeof window.configurePlugin,
|
||||
checkStatus: typeof window.checkStatus
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user