1 // Listen to messages from our content-script
2 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
5 // If we're receiving a message with a query, search BookStack
6 // and return the BookStack results in the response.
8 searchBookStack(request.query).then(results => {
10 sendResponse({results});
15 // Return true enables 'sendResponse' to work async
20 // Search our BookStack instance using the given query
21 async function searchBookStack(query) {
23 // Load BookStack API details from our options
24 const options = await loadOptions();
25 for (const option of Object.values(options)) {
27 console.log('Missing a required option');
32 // Query BookStack, making an authorized API search request
33 const url = `${options.baseUrl}/api/search?query=${encodeURIComponent(query)}`;
34 const resp = await fetch(url, {
37 Authorization: `Token ${options.tokenId}:${options.tokenSecret}`,
41 // Parse the JSON response and return the results
42 const data = await resp.json();
43 return data.data || null;
48 * Load our options from chrome's storage.
49 * @returns Promise<Object>
51 function loadOptions() {
52 return new Promise((res, rej) => {
53 chrome.storage.sync.get({