58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
document.addEventListener('DOMContentLoaded', function () {
|
|
let currentKA = null;
|
|
|
|
console.log('Freshdesk app loaded');
|
|
|
|
// Listen for messages from the KB iframe
|
|
window.addEventListener('message', function(event) {
|
|
console.log('Received message:', event.origin, event.data);
|
|
|
|
if (event.origin !== 'https://jvis.freshdesk.com') {
|
|
console.log('Ignoring message from wrong origin');
|
|
return;
|
|
}
|
|
|
|
if (event.data.type === 'ka-viewed') {
|
|
currentKA = event.data.kaNumber;
|
|
console.log('Current KA set to:', currentKA);
|
|
}
|
|
});
|
|
|
|
app.initialized()
|
|
.then(function (client) {
|
|
console.log('KB app initialized');
|
|
|
|
document.getElementById('insert-ka-btn').addEventListener('click', function() {
|
|
console.log('Insert button clicked, currentKA:', currentKA);
|
|
|
|
if (!currentKA) {
|
|
client.interface.trigger('showNotify', {
|
|
type: 'warning',
|
|
message: 'No article selected. Please view an article first.'
|
|
});
|
|
return;
|
|
}
|
|
|
|
client.interface.trigger("setValue", {
|
|
id: "cf_ka_number",
|
|
value: currentKA
|
|
})
|
|
.then(function(data) {
|
|
return client.interface.trigger('showNotify', {
|
|
type: 'success',
|
|
message: 'KA# ' + currentKA + ' attached'
|
|
});
|
|
})
|
|
.catch(function(error) {
|
|
client.interface.trigger('showNotify', {
|
|
type: 'error',
|
|
message: 'Failed to attach KA#: ' + error.message
|
|
});
|
|
});
|
|
});
|
|
})
|
|
.catch(function(error) {
|
|
console.error('Failed to initialize app:', error);
|
|
document.body.innerHTML = '<p>Failed to load app. Please refresh.</p>';
|
|
});
|
|
}); |