-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
142 lines (124 loc) · 4.23 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
// General URL = https://docs.google.com/presentation/d/15t24M-o_PGMFdkZ-Q6xbPucWJMXKGHyVrUYWkTCnyEI/edit#slide=id.g2ebc14ed5c9_0_221
const options = [
{
id: "copy-document-id",
name: "Copy document id",
icon: "docs-icon-editors-ia-content-copy",
action: () => {
const url = window.location.href;
const id = url.match(/d\/(.*)\/edit/)[1];
navigator.clipboard.writeText(id);
console.log(`Document ID copied: ${id}`);
createAlert(`Document ID copied: ${id}`);
}
},
{
id: "copy-slide-id",
name: "Copy slide id",
icon: "docs-icon-editors-ia-content-copy",
action: () => {
const url = window.location.href;
const id = url.match(/slide=id\.(.*)/)[1];
navigator.clipboard.writeText(id);
console.log(`Slide ID copied: ${id}`);
createAlert(`Slide ID copied: ${id}`);
}
},
{
id: "goto-slide-id",
name: "Go to copied slide id",
icon: "docs-icon-editors-ia-view-show",
action: () => {
const copied = navigator.clipboard.readText();
copied.then(id => {
const url = window.location.href;
const newUrl = url.replace(/slide=id\..*/, `slide=id.${id}`);
window.location.href = newUrl;
// Check if the slide id is valid
const escapedId = CSS.escape(id);
const slide = document.querySelector(`[id$="${escapedId}"]`);
if (slide) {
console.log(`Navigating to slide id: ${id}`);
createAlert(`Navigating to slide id: ${id}`);
} else {
console.log(`Invalid slide id: ${id}`);
createAlert(`Invalid slide id: ${id}`, false);
}
})
}
}
]
/**
*
* @param {string} message Message to display
* @param {boolean} success If the alert is a success or error, default is success
*/
const createAlert = (message, success = true) => {
const alert = document.createElement("div");
alert.style.position = "fixed";
alert.style.top = "10px";
alert.style.left = "10px";
alert.style.padding = "10px";
alert.style.borderRadius = "5px";
alert.style.backgroundColor = "#333";
alert.style.color = "#fff";
alert.style.transition = "opacity 0.5s";
alert.style.zIndex = "99999";
alert.className = "custom-alert";
alert.innerHTML = `
<span>${success ? "✅" : "❌"} ${message}</span>
`;
document.body.appendChild(alert);
setTimeout(() => {
alert.style.opacity = 0;
setTimeout(() => {
alert.remove();
}, 500);
}, 2000);
}
document.addEventListener("click", () => {
const menus = document.querySelectorAll(".apps-menu-hide-mnemonics");
menus.forEach(menu => {
if (!menu.querySelector(".custom-menu-item")) {
options.forEach(({id, name, icon, action, disabled}) => {
const customItem = document.createElement("div");
customItem.className = `goog-menuitem apps-menuitem custom-menu-item custom-menu-item-${id}`;
customItem.role = "menuitem";
customItem.style.userSelect = "none";
customItem.innerHTML = `
<div class="goog-menuitem-content">
<div
class="docs-icon goog-inline-block goog-menuitem-icon"
aria-hidden="true">
<div class="docs-icon-img-container docs-icon-img ${icon}"></div>
</div>
<span class="goog-menuitem-label">${name}</span>
</div>
`;
if (disabled) {
customItem.classList.add("goog-menuitem-disabled");
customItem.tooltip = "Disabled";
} else {
customItem.addEventListener("click", () => {
action();
menu.style.display = "none";
});
customItem.addEventListener("mouseover", () => {
customItem.classList.add("goog-menuitem-highlight");
});
customItem.addEventListener("mouseout", () => {
customItem.classList.remove("goog-menuitem-highlight");
});
}
menu.appendChild(customItem);
});
const separator = document.createElement("div");
separator.className = "goog-menuseparator";
separator.ariaDisabled = "true";
separator.role = "separator";
separator.id = "4wn7q5:1c0";
separator.style.userSelect = "none";
menu.appendChild(separator);
}
});
})