-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
126 lines (112 loc) · 3.61 KB
/
content.js
File metadata and controls
126 lines (112 loc) · 3.61 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
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
function getActionItems() {
return document.querySelector(".pagehead-actions");
}
function getIcon(application) {
switch (application) {
case "Cursor":
return fetch(chrome.runtime.getURL("/icons/cursor.svg")).then(
(response) => response.text(),
);
case "VS Code":
return fetch(chrome.runtime.getURL("/icons/vscode.svg")).then(
(response) => response.text(),
);
case "VS Code Insiders":
return fetch(chrome.runtime.getURL("/icons/vscode-insiders.svg")).then(
(response) => response.text(),
);
case "VSCodium":
return fetch(chrome.runtime.getURL("/icons/vscodium.svg")).then(
(response) => response.text(),
);
case "VSCodium Insiders":
return fetch(chrome.runtime.getURL("/icons/vscodium-insiders.svg")).then(
(response) => response.text(),
);
default:
return Promise.resolve("");
}
}
function createOpenInEditorItem(application) {
const listItem = document.createElement("li");
const editorButton = createEditorButton(application);
listItem.appendChild(editorButton);
return listItem;
}
function appendOpenEditorButton(application) {
const actionItems = getActionItems();
if (actionItems) {
const openInEditorItem = createOpenInEditorItem(application);
actionItems.appendChild(openInEditorItem);
}
}
function getEditorLink(callback) {
const url = window.location.href;
const domains = [
"github.com",
"gitlab.com",
"bitbucket.org",
"dev.azure.com",
];
for (const domain of domains) {
const regex = new RegExp(`https://${domain}/(.*)`);
const matches = url.match(regex);
if (!matches) continue;
chrome.storage.sync.get(
{
protocol: "HTTPS",
application: "Cursor",
},
function (items) {
const domainMatch = url.match(/https:\/\/([^/]+)\//);
if (!domainMatch) return;
const domain = domainMatch[1];
const appProtocol =
{
Cursor: "cursor",
"VS Code": "vscode",
"VS Code Insiders": "vscode-insiders",
VSCodium: "vscodium",
"VSCodium Insiders": "vscodium-insiders",
}[items.application] || "cursor";
let cloneUrl = `${items.protocol.toLowerCase()}://${domain}/${
matches[1]
}`;
if (matches[1].startsWith("tree/")) {
cloneUrl = cloneUrl.replace(/tree\/[^/]+\//, "");
}
cloneUrl += ".git";
const vscodeUrl = `${appProtocol}://vscode.git/clone?url=${cloneUrl}`;
callback(vscodeUrl, items.application);
},
);
}
}
function createEditorButton(application) {
const editorButton = document.createElement("span");
editorButton.className = "btn btn-sm";
editorButton.setAttribute("aria-label", `Open repository in ${application}`);
const buttonTitle = document.createElement("span");
buttonTitle.textContent = `Open in ${application}`;
buttonTitle.style.marginLeft = '5px';
editorButton.style.display = 'flex';
editorButton.style.flexDirection = 'row';
editorButton.style.alignItems = 'center';
getIcon(application).then((icon) => {
const iconContainer = document.createElement("span");
iconContainer.innerHTML = icon;
iconContainer.firstChild.style.verticalAlign = 'middle';
editorButton.appendChild(iconContainer);
editorButton.appendChild(buttonTitle);
});
getEditorLink((vscodeUrl) => {
editorButton.onclick = () => {
window.open(vscodeUrl, '_blank');
};
editorButton.title = vscodeUrl;
});
return editorButton;
}
getEditorLink((vscodeUrl, application) => {
appendOpenEditorButton(application);
});