-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent.js
59 lines (50 loc) · 1.61 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
function getBaseUrl(projectName) {
return `https://scrapbox.io/${projectName}/`;
}
function getSearchUrl(projectName) {
const searchQuery = new URL(window.location.href).searchParams.get("q");
return `https://scrapbox.io/api/pages/${projectName}/search/query?q=${searchQuery}`;
}
function showResults(response, projectName) {
const baseUrl = getBaseUrl(projectName);
const data = response.pages;
const container = document.createElement("div");
container.className = "scrapbox-search-results";
container.innerHTML = `<h2>search results in scrapbox.io/${projectName}:</h2>`;
if (!data.length) {
container.innerHTML += `<p>No results found.</p>`;
} else {
data.forEach((page) => {
container.innerHTML += `<a href="${
baseUrl + page.title
}" target="_blank">${page.title}</a><span> / </span>`;
});
}
if (document.getElementById("rhs")) {
document.getElementById("rhs").prepend(container);
} else {
const rhs = document.createElement("div");
rhs.setAttribute("id", "rhs");
document.getElementById("rcnt").appendChild(rhs);
document.getElementById("rhs").prepend(container);
}
}
function search(projects, sids) {
if (!projects) return;
for (let i = 0; i < projects.length; i++) {
const searchUrl = getSearchUrl(projects[i]);
chrome.runtime.sendMessage(
{
contentScriptQuery: "post",
endpoint: searchUrl,
sid: sids[i],
},
(response) => {
showResults(response, projects[i]);
}
);
}
}
chrome.storage.local.get(["projects", "sids"], function (items) {
search(items.projects, items.sids);
});