78 lines
3.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://kb.jv.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');
// Check if ticket already has a KA number and navigate to it
client.data.get('ticket')
.then(function(data) {
const existingKA = data.ticket.custom_fields.cf_ka_number;
console.log('Existing KA in ticket:', existingKA);
if (existingKA) {
// Send message to KB iframe to navigate to this article
const iframe = document.getElementById('kb-iframe');
iframe.contentWindow.postMessage({
type: 'navigate-to-ka',
kaNumber: existingKA
}, 'https://kb.jv.com');
}
})
.catch(function(error) {
console.error('Error reading ticket:', error);
});
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) {
if (data) {}
return client.interface.trigger('showNotify', {
type: 'success',
message: currentKA + ' attached to ticket'
});
})
.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>';
});
});