diff --git a/background.js b/background.js index 4d5eeaf..3f712d4 100644 --- a/background.js +++ b/background.js @@ -1,37 +1,39 @@ -// Message listener for various actions browser.runtime.onMessage.addListener((message, sender, sendResponse) => { if (message.action === 'searchMastodon') { // Load saved settings from browser.storage.local - browser.storage.local.get(['client_id', 'client_secret', 'access_token', 'domain', 'numPosts']) - .then(({ client_id, client_secret, access_token, domain, numPosts = 5 }) => { - console.log(access_token); - console.log(domain); - if (!access_token || !domain) { - sendResponse({ success: false, error: 'Missing access token or domain.' }); + browser.storage.local.get(['client_id', 'client_secret', 'access_token', 'apiKey', 'domain', 'numPosts']) + .then(({ client_id, client_secret, access_token, apiKey, domain, numPosts = 5 }) => { + if ((!access_token && !apiKey) || !domain) { + sendResponse({ success: false, error: 'Missing access token, API key, or domain.' }); return; } const searchTerm = message.searchTerm; + const authorization = access_token ? `Bearer ${access_token}` : `Bearer ${apiKey}`; + + console.log(authorization); + fetch(`https://${domain}/api/v2/search?q=${encodeURIComponent(searchTerm)}&resolve=true&limit=${numPosts}`, { headers: { - 'Authorization': `Bearer ${access_token}` + 'Authorization': authorization } }) .then(response => response.json()) .then(data => { - console.log(data.statuses); //TODO: For some reason this sendResponse does not work unless I log data.statuses. I have no idea why. Best guess is there's some sort of race condition going on? Though I'm not familiar enough with javascript to know if that's true + console.log(data.statuses); //TODO: For some reason this sendResponse does not work unless I log data.statuses. I have no idea why. Best guess is there's some sort of race condition going on? Though I'm not familiar enough with javascript to know if that's true sendResponse({ success: true, results: data.statuses }); }) .catch(error => { sendResponse({ success: false, error: error.message }); }); - return true; + return true; }) .catch(error => { sendResponse({ success: false, error: 'Failed to retrieve saved settings.' }); }); - return true; + + return true; } else if (message.action === 'getSettings') { browser.storage.local.get(['domain', 'numPosts']) .then(({ domain, numPosts = 5 }) => { diff --git a/manifest.json b/manifest.json index 0d94028..95b87ee 100644 --- a/manifest.json +++ b/manifest.json @@ -1,8 +1,8 @@ { "manifest_version": 2, "name": "DuckDuckSocial", - "version": "0.0.3", - "description": "Adds a scrollable list of posts from the social web that match your DuckDuckGo search. Requires API key from mastodon compatible server", + "version": "0.0.4", + "description": "Adds a scrollable list of posts from the social web that match your DuckDuckGo search. Mobile support requires API key", "icons": { "512": "icons/border-512.png" }, diff --git a/options.js b/options.js index 747ebaa..050f2d6 100644 --- a/options.js +++ b/options.js @@ -2,7 +2,10 @@ document.addEventListener('DOMContentLoaded', function() { const form = document.getElementById('settingsForm'); const domainInput = document.getElementById('domain'); const numPostsInput = document.getElementById('numPosts'); + const apiKeyInput = document.getElementById('apiKey'); const messageElement = document.querySelector('.message'); + const toggleButton = document.getElementById('toggleAdvancedSettings'); + const advancedSettings = document.querySelector('.advanced-settings'); // Load saved settings loadSettings(); @@ -11,6 +14,12 @@ document.addEventListener('DOMContentLoaded', function() { event.preventDefault(); saveSettings(); }); + + toggleButton.addEventListener('click', function() { + const isVisible = advancedSettings.style.display === 'block'; + advancedSettings.style.display = isVisible ? 'none' : 'block'; + toggleButton.textContent = isVisible ? 'Show Advanced Settings' : 'Hide Advanced Settings'; + }); document.getElementById('connectMastodon').addEventListener('click', function(event) { const domain = domainInput.value; @@ -38,10 +47,12 @@ document.addEventListener('DOMContentLoaded', function() { function saveSettings() { const domain = domainInput.value; const numPosts = numPostsInput.value; + const apiKey = apiKeyInput.value; browser.storage.local.set({ domain: domain, - numPosts: numPosts + numPosts: numPosts, + apiKey: apiKey // Save the API key as well }).then(() => { updateMessage('Settings saved successfully!'); }).catch(error => { @@ -49,12 +60,16 @@ document.addEventListener('DOMContentLoaded', function() { updateMessage('Failed to save settings.'); }); } - + function loadSettings() { - browser.storage.local.get(['domain', 'numPosts']) - .then(({ domain, numPosts = 5 }) => { + browser.storage.local.get(['domain', 'numPosts', 'apiKey']) + .then(({ domain, numPosts = 5, apiKey }) => { if (domain) domainInput.value = domain; if (numPosts) numPostsInput.value = numPosts; + if (apiKey) { + apiKeyInput.value = apiKey; + advancedSettingsDiv.style.display = 'block'; + } }) .catch(error => { console.error('Failed to load settings:', error); @@ -66,3 +81,5 @@ document.addEventListener('DOMContentLoaded', function() { messageElement.style.color = 'red'; } }); + + diff --git a/settings.html b/settings.html index 085483f..52d3409 100644 --- a/settings.html +++ b/settings.html @@ -17,6 +17,9 @@ box-sizing: border-box; margin-bottom: 20px; } + .advanced-settings { + display: none; + } @@ -27,11 +30,18 @@

DuckDuckSocial

- + + +
+ + +
+ + -

+