-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
89 lines (79 loc) · 3.14 KB
/
script.js
File metadata and controls
89 lines (79 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
document.addEventListener('DOMContentLoaded', () => {
populateObjectList();
});
// List of SOAP API objects
const sfmcObjects = ['Account', 'AccountUser', 'Automation',
'BounceEvent', 'ClickEvent','DataExtension',
'ListSubscribers','NotSentEvent','OpenEvent',
'QueryDefinition','SendSummary','SentEvent',
'Subscribers','TriggeredSendSummary','UnsubEvent'];
// Function to populate the object list
function populateObjectList() {
const listContainer = document.getElementById('object-list');
sfmcObjects.forEach(obj => {
const listItem = document.createElement('div');
listItem.className = 'list-item';
listItem.innerHTML = `
<input type="radio" name="sfmcObject" value="${obj}" id="${obj}">
<label for="${obj}">${obj}</label>
`;
listItem.addEventListener('click', () => selectItem(listItem, obj));
listContainer.appendChild(listItem);
});
}
// Function to handle item selection
function selectItem(item, objectName) {
// Remove 'selected' class from all items
const allItems = document.querySelectorAll('.list-item');
allItems.forEach(i => i.classList.remove('selected'));
// Add 'selected' class to the clicked item
item.classList.add('selected');
// Update button text
const button = document.getElementById('generate-code');
button.textContent = `Generate Code for ${objectName}`;
}
// Function to fetch code from GitHub
async function fetchCodeFromGitHub(objectName) {
const githubUrl = `https://api.github.com/repos/b2Shashi-mc/ampscript-soap-api/contents/${objectName}.amp`;
try {
const response = await fetch(githubUrl, {
headers: {
'Accept': 'application/vnd.github.v3.raw' // Fetch raw content
}
});
if (!response.ok) throw new Error('Failed to fetch code');
return await response.text();
} catch (error) {
alert("Error fetching code. Please try again later.");
console.error(error);
return null;
}
}
// Function to copy text to clipboard
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
const modal = document.getElementById("copyModal");
modal.classList.add("show");
// After 3 seconds, fade out the modal
setTimeout(() => {
modal.classList.remove("show");
}, 2000); // 2 seconds
}).catch(err => {
console.error('Failed to copy text: ', err);
});
}
// Event listener for the "Generate Code" button
document.getElementById('generate-code').addEventListener('click', async () => {
const selectedObject = document.querySelector('input[name="sfmcObject"]:checked');
if (!selectedObject) {
alert('Please select a SOAP API object.');
return;
}
const objectName = selectedObject.value;
const code = await fetchCodeFromGitHub(objectName);
if (code) {
copyToClipboard(code);
} else {
alert('Failed to fetch or copy code.');
}
});