-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
80 lines (75 loc) · 2.2 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
const searchQuery = new URL(window.location.href).searchParams.get("q");
function showNotionResults(results) {
const resultsContainer = document.createElement("div");
resultsContainer.className = "notion-search-results";
resultsContainer.innerHTML = `
<h2>Notion Search Results</h2>
${
results.length
? results
.map((result) => {
const titleProperty = Object.values(result.properties).find(
(property) => property.type === "title"
);
const title =
titleProperty?.title?.[0]?.plain_text || "No Title Found";
return `
<a href="${result.url}" target="_blank">
${title}
</a> /
`;
})
.join("")
: "<p>No results found</p>"
}
`;
const rhs = document.getElementById("rhs");
const rcnt = document.getElementById("rcnt");
if (rhs) {
rhs.prepend(resultsContainer);
} else if (rcnt) {
const newRhs = document.createElement("div");
newRhs.setAttribute("id", "rhs");
rcnt.appendChild(newRhs);
newRhs.prepend(resultsContainer);
} else {
console.error(
"Unable to find the target element to append the search results."
);
}
}
if (searchQuery) {
chrome.storage.sync.get(["notionApiToken"], (data) => {
const NOTION_API_TOKEN = data.notionApiToken;
if (NOTION_API_TOKEN) {
chrome.runtime.sendMessage(
{
action: "fetchData",
url: "https://api.notion.com/v1/search",
token: NOTION_API_TOKEN,
body: {
query: searchQuery,
sort: {
direction: "ascending",
timestamp: "last_edited_time",
},
},
},
(response) => {
if (response.error) {
console.error(response.error);
} else {
const results = response.data.results;
if (results.length > 0) {
showNotionResults(results);
} else {
showNotionResults(results, null);
}
}
}
);
} else {
console.error("Notion API Token is not set.");
}
});
}