forked from andyjphu/studai-teacher-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
145 lines (94 loc) · 3.84 KB
/
content.js
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
function checkUrl() {
return window.location.href.includes ('canvas');
}
function isCanvasAssignmentCreationPage() {
console.log("WENT THROUGH ASSIGNMENT CREATION PAGE")
console.log(window.location.href.includes('/assignments'))
console.log(window.location.href.includes('/edit'))
console.log(window.location.href.includes('/new'))
return window.location.href.includes('/assignments') && (window.location.href.includes('/new') || (window.location.href.includes('/edit')));
}
function isCanvasAssignmentViewPage() {
return window.location.href.includes("/assignments") && (!window.location.href.endsWith('/assignments'));
}
async function backendRequest(url, data) {
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
mode: 'no-cors', // This is the key setting
body: new URLSearchParams(data),
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const result = await response.text();
return result;
} catch (error) {
console.error('Error:', error);
throw error; // Re-throw the error if needed
}
}
// // Example usage
// backendRequest('https://www.example.com/api', { key1: 'value1', key2: 'value2' })
// .then(result => console.log(result))
// .catch(error => console.error('Request failed:', error));
function main() {
if (!checkUrl()) {
console.error('This is not a Canvas assignment creation page.');
return;
}
// Create a new div element with your content
const newDiv = document.createElement('div');
newDiv.innerHTML = `
<div>Estimate Time</div>
`;
function handleClick() {
// alert('Div clicked!');
backendRequest('https://google.com', JSON.stringify(document.querySelector("#assignment_show").innerHTML) )
.then(result => console.log(result))
.catch(error => console.error('Request failed:', error));
// You can also add other functionality here
}
// Add an event listener to the div
newDiv.addEventListener('click', handleClick);
if (isCanvasAssignmentCreationPage()) {
newDiv.id = 'studai-canvas-button-2';
const targetElement = document.querySelector('#edit_assignment_header > div.header-bar.assignment-edit-header > div');
if (targetElement) {
//targetElement.appendChild(newDiv)
targetElement.insertBefore(newDiv, targetElement.firstChild);
} else {
console.error('1: Target element not found.');
}
}
else if (isCanvasAssignmentViewPage()) {
newDiv.id = 'studai-canvas-button-3';
const targetElement = document.querySelector('#assignment_show > div.assignment-title > div.assignment-buttons');
// Check if the target element exists before appending
if (targetElement) {
// Get the second-to-last child of the target element
const children = targetElement.children;
const secondToLastChild = children[children.length - 2];
// Check if there is at least two children
if (secondToLastChild) {
// Insert the new element before the second-to-last child
targetElement.insertBefore(newDiv, secondToLastChild);
} else {
// If there are fewer than two children, append to the end
targetElement.appendChild(newDiv);
}
} else {
console.error('2: Target element not found.');
}
}
else {
console.error('all cases passed, no match');
return;
}
}
console.log("running?")
window.addEventListener('load', main);
console.log("ran?")