Skip to content

Commit 5cea7a8

Browse files
committed
use requestUrl over fetch; cleanup settings section
1 parent bb14c33 commit 5cea7a8

File tree

1 file changed

+30
-37
lines changed

1 file changed

+30
-37
lines changed

src/main.js

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Plugin, PluginSettingTab, Setting } = require('obsidian');
1+
const { Plugin, PluginSettingTab, Setting, requestUrl } = require('obsidian');
22

33
const DEFAULT_SETTINGS = {
44
apiKey: '',
@@ -184,17 +184,20 @@ module.exports = class PexelsBannerPlugin extends Plugin {
184184

185185
for (const currentKeyword of keywords) {
186186
try {
187-
const response = await fetch(`https://api.pexels.com/v1/search?query=${currentKeyword}&per_page=${this.settings.numberOfImages}&size=${this.settings.imageSize}&orientation=${this.settings.imageOrientation}`, {
187+
const response = await requestUrl({
188+
url: `https://api.pexels.com/v1/search?query=${encodeURIComponent(currentKeyword)}&per_page=${this.settings.numberOfImages}&size=${this.settings.imageSize}&orientation=${this.settings.imageOrientation}`,
189+
method: 'GET',
188190
headers: {
189-
Authorization: this.settings.apiKey
191+
'Authorization': this.settings.apiKey
190192
}
191193
});
192194

193-
if (!response.ok) {
194-
throw new Error(`HTTP error! status: ${response.status}`);
195+
if (response.status !== 200) {
196+
console.error('Failed to fetch images:', response.status, response.text);
197+
return null;
195198
}
196199

197-
const data = await response.json();
200+
const data = response.json;
198201

199202
if (data.photos && data.photos.length > 0) {
200203
const randomIndex = Math.floor(Math.random() * data.photos.length);
@@ -244,9 +247,6 @@ class PexelsBannerSettingTab extends PluginSettingTab {
244247
containerEl.empty();
245248
containerEl.addClass('pexels-banner-settings');
246249

247-
const header = containerEl.createEl('div', {cls: 'pexels-banner-header'});
248-
header.createEl('h2', {text: 'Pexels Banner Settings'});
249-
250250
// New section: About this plugin
251251
const aboutSection = containerEl.createEl('div', {cls: 'pexels-banner-section'});
252252
aboutSection.createEl('h3', {text: 'About Pexels Banner'});
@@ -268,20 +268,8 @@ class PexelsBannerSettingTab extends PluginSettingTab {
268268
const mainContent = containerEl.createEl('div', {cls: 'pexels-banner-main-content'});
269269

270270
// API Key section
271-
const apiKeySection = mainContent.createEl('div', {cls: 'pexels-banner-section'});
272-
apiKeySection.createEl('h3', {text: 'Pexels API Key'});
273-
const apiKeySteps = apiKeySection.createEl('ol');
274-
apiKeySteps.createEl('li').createEl('a', {
275-
text: 'https://www.pexels.com/api/',
276-
href: 'https://www.pexels.com/api/',
277-
target: '_blank',
278-
rel: 'noopener'
279-
});
280-
apiKeySteps.createEl('li', {text: 'Signup for a free account or login'});
281-
apiKeySteps.createEl('li', {text: 'Create / Copy your API key and paste it below'});
282-
283-
new Setting(apiKeySection)
284-
.setName('API Key')
271+
new Setting(mainContent)
272+
.setName('API key')
285273
.setDesc('Enter your Pexels API key')
286274
.addText(text => text
287275
.setPlaceholder('Enter your API key')
@@ -295,12 +283,16 @@ class PexelsBannerSettingTab extends PluginSettingTab {
295283
setting.controlEl.querySelector('input').style.width = '100%';
296284
});
297285

298-
// Image Settings section
299-
const imageSettingsSection = mainContent.createEl('div', {cls: 'pexels-banner-section'});
300-
imageSettingsSection.createEl('h3', {text: 'Image Settings'});
286+
// Add spacing after the API Key section
287+
mainContent.createEl('div', {cls: 'pexels-banner-spacing'});
301288

302-
new Setting(imageSettingsSection)
303-
.setName('Image Size')
289+
// Image settings section
290+
new Setting(mainContent)
291+
.setName('Images')
292+
.setHeading();
293+
294+
new Setting(mainContent)
295+
.setName('Size')
304296
.setDesc('Select the size of the image')
305297
.addDropdown(dropdown => dropdown
306298
.addOption('small', 'Small')
@@ -312,8 +304,8 @@ class PexelsBannerSettingTab extends PluginSettingTab {
312304
await this.plugin.saveSettings();
313305
}));
314306

315-
new Setting(imageSettingsSection)
316-
.setName('Image Orientation')
307+
new Setting(mainContent)
308+
.setName('Orientation')
317309
.setDesc('Select the orientation of the image')
318310
.addDropdown(dropdown => dropdown
319311
.addOption('landscape', 'Landscape')
@@ -325,8 +317,8 @@ class PexelsBannerSettingTab extends PluginSettingTab {
325317
await this.plugin.saveSettings();
326318
}));
327319

328-
new Setting(imageSettingsSection)
329-
.setName('Number of Images')
320+
new Setting(mainContent)
321+
.setName('Number of images')
330322
.setDesc('Enter the number of random images to fetch (1-50)')
331323
.addText(text => text
332324
.setPlaceholder('10')
@@ -346,9 +338,8 @@ class PexelsBannerSettingTab extends PluginSettingTab {
346338
inputEl.style.width = '50px';
347339
});
348340

349-
// Add new setting for default keywords
350-
new Setting(imageSettingsSection)
351-
.setName('Default Keywords')
341+
new Setting(mainContent)
342+
.setName('Default keywords')
352343
.setDesc('Enter a comma-separated list of default keywords to be used when no keyword is provided in the frontmatter, or when the provided keyword does not return any results.')
353344
.addTextArea(text => text
354345
.setPlaceholder('Enter keywords, separated by commas')
@@ -365,8 +356,11 @@ class PexelsBannerSettingTab extends PluginSettingTab {
365356
});
366357

367358
// How to use section
359+
new Setting(mainContent)
360+
.setName('How to use')
361+
.setHeading();
362+
368363
const instructionsEl = mainContent.createEl('div', {cls: 'pexels-banner-section'});
369-
instructionsEl.createEl('h3', {text: 'How to Use'});
370364
instructionsEl.createEl('p', {text: 'Add a "pexels-banner" field to your note\'s frontmatter with keywords for the image you want, or a direct URL to an image.'});
371365
const codeEl = instructionsEl.createEl('pre');
372366
codeEl.createEl('code', {text:
@@ -381,7 +375,6 @@ pexels-banner: https://example.com/image.jpg
381375
---`
382376
});
383377

384-
385378
// Footer
386379
const footerEl = containerEl.createEl('div', {cls: 'pexels-banner-footer'});
387380
footerEl.createEl('p', {

0 commit comments

Comments
 (0)