-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
87 lines (74 loc) · 3.13 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
// Function to toggle URL mode and retain scroll position
function toggleMode(button) {
const currentUrl = window.location.href;
// Capture the current scroll position
const scrollPosition = {
x: window.scrollX,
y: window.scrollY,
};
// Save the scroll position in sessionStorage
sessionStorage.setItem("scrollPosition", JSON.stringify(scrollPosition));
if (currentUrl.includes("?mode=view")) {
// Switch to edit mode
const newUrl = currentUrl.replace("?mode=view", "?mode=edit");
window.location.href = newUrl; // Redirect to the new URL
} else if (currentUrl.includes("?mode=edit")) {
// Switch back to view mode
const newUrl = currentUrl.replace("?mode=edit", "?mode=view");
window.location.href = newUrl; // Redirect to the new URL
}
}
// Function to update the button text based on the current mode
function updateButtonText(button) {
const currentUrl = window.location.href;
if (currentUrl.includes("?mode=view")) {
button.textContent = "🖉";
} else if (currentUrl.includes("?mode=edit")) {
button.textContent = "View";
}
}
// Restore scroll position after a short delay to allow page to settle
function restoreScrollPosition() {
const scrollPosition = sessionStorage.getItem("scrollPosition");
if (scrollPosition) {
const { x, y } = JSON.parse(scrollPosition);
// Introduce a delay to ensure the page layout is settled before restoring scroll
setTimeout(() => {
window.scrollTo(x, y);
sessionStorage.removeItem("scrollPosition"); // Clear the stored position after restoring
}, 500); // Adjust the delay time if needed (in milliseconds)
}
}
// Check if the floating button already exists to avoid duplicates
if (
!document.getElementById("urlModeSwitcherButton") &&
(window.location.href.includes("experiments.php?mode=view") ||
window.location.href.includes("experiments.php?mode=edit") ||
window.location.href.includes("database.php?mode=view") ||
window.location.href.includes("database.php?mode=edit"))
) {
// Inject the CSS file
function injectCSS(filename) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = chrome.runtime.getURL(filename);
document.head.appendChild(link);
}
injectCSS("style.css");
// Create the button
const button = document.createElement("button");
button.id = "urlModeSwitcherButton";
// Set initial button text based on the current mode
updateButtonText(button);
// Add the button to the document
document.body.appendChild(button);
// Add click event listener to toggle between modes
button.addEventListener("click", () => {
toggleMode(button);
// Update the button text after the URL changes
setTimeout(() => updateButtonText(button), 100); // Slight delay to ensure URL changes
});
// Restore scroll position on page load
restoreScrollPosition();
}