Skip to content

Commit 39e246b

Browse files
authored
Merge pull request #2 from kxvinthxngxvel/dev
Dev
2 parents 7645a05 + 767a521 commit 39e246b

6 files changed

Lines changed: 67 additions & 56 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
node_modules/
33
dist/
4+
.vercel

api/handlerq.js

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
export default async function handler(req, res) {
2-
// Add CORS headers to allow requests from any origin
3-
res.setHeader("Access-Control-Allow-Origin", "*"); // Allow all origins
4-
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); // Allow GET and OPTIONS methods
5-
res.setHeader("Access-Control-Allow-Headers", "Content-Type"); // Allow Content-Type header
6-
7-
// Handle preflight request (OPTIONS)
8-
if (req.method === "OPTIONS") {
9-
return res.status(200).end(); // Return 200 for preflight requests
10-
}
11-
12-
// Define the Quotable API URL
13-
const QUOTABLE_API_URL = "https://api.quotable.io/random?tags=motivational";
14-
15-
try {
16-
console.log("Fetching a quote from Quotable API...");
17-
const response = await fetch(QUOTABLE_API_URL);
18-
19-
console.log("Quotable API Response Status:", response.status);
20-
if (!response.ok) {
21-
const errorData = await response.json();
22-
console.error("Error Response from Quotable API:", errorData);
23-
return res.status(response.status).json({
24-
error: errorData?.message || "Failed to fetch quote from Quotable API",
25-
});
26-
}
27-
28-
const quoteData = await response.json();
29-
console.log("Received data from Quotable API:", quoteData);
30-
31-
// Check if the response contains valid quote data
32-
if (!quoteData || !quoteData.content || !quoteData.author) {
33-
console.error("Invalid quote data from Quotable API:", quoteData);
34-
return res.status(404).json({ error: "No valid quote found from Quotable API" });
35-
}
36-
37-
// Return the quote and author to the client
38-
res.status(200).json({
39-
quote: quoteData.content,
40-
author: quoteData.author,
2+
// Add CORS headers to allow requests from any origin
3+
res.setHeader("Access-Control-Allow-Origin", "*"); // Allow all origins
4+
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); // Allow GET and OPTIONS methods
5+
res.setHeader("Access-Control-Allow-Headers", "Content-Type"); // Allow Content-Type header
6+
7+
// Handle preflight request (OPTIONS)
8+
if (req.method === "OPTIONS") {
9+
return res.status(200).end(); // Return 200 for preflight requests
10+
}
11+
12+
// Define the ZenQuotes API URL
13+
const ZENQUOTES_API_URL = "https://zenquotes.io/api/random";
14+
15+
try {
16+
console.log("Fetching a quote from ZenQuotes API...");
17+
const response = await fetch(ZENQUOTES_API_URL);
18+
19+
console.log("ZenQuotes API Response Status:", response.status);
20+
if (!response.ok) {
21+
const errorData = await response.json();
22+
console.error("Error Response from ZenQuotes API:", errorData);
23+
return res.status(response.status).json({
24+
error: errorData?.message || "Failed to fetch quote from ZenQuotes API",
4125
});
42-
} catch (error) {
43-
console.error("Error fetching quote from Quotable API:", error);
44-
// Return a detailed error message
45-
res.status(500).json({ error: `Failed to fetch quote from Quotable API: ${error.message}` });
4626
}
27+
28+
const quoteData = await response.json();
29+
console.log("Received data from ZenQuotes API:", quoteData);
30+
31+
// Check if the response contains valid quote data
32+
if (!quoteData || !quoteData[0]?.q || !quoteData[0]?.a) {
33+
console.error("Invalid quote data from ZenQuotes API:", quoteData);
34+
return res.status(404).json({ error: "No valid quote found from ZenQuotes API" });
35+
}
36+
37+
// Return the quote and author to the client
38+
res.status(200).json({
39+
quote: quoteData[0].q, // The quote text
40+
author: quoteData[0].a, // The author name
41+
});
42+
} catch (error) {
43+
console.error("Error fetching quote from ZenQuotes API:", error);
44+
// Return a detailed error message
45+
res.status(500).json({ error: `Failed to fetch quote from ZenQuotes API: ${error.message}` });
4746
}
48-
47+
}

src/background.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ chrome.runtime.onInstalled.addListener(() => {
55
async function fetchImageAndQuote() {
66
// Updated API URLs
77
const UNSPLASH_API_URL = "https://motivision.vercel.app/api/handler.js"; // Vercel-hosted API for image
8-
const QUOTE_API_URL = "https://motivision.vercel.app/api/handlerq.js"; // Existing quote API
8+
const QUOTE_API_URL = "https://motivision.vercel.app/api/handlerq.js"; // Your ZenQuotes API URL
99

1010
try {
1111
// Fetch image and quote in parallel
1212
const [imageData, quoteData] = await Promise.all([
1313
fetch(UNSPLASH_API_URL).then(res => res.json()), // Fetch image from Vercel API
14-
fetch(QUOTE_API_URL).then(res => res.json()) // Fetch quote from quotable.io API
14+
fetch(QUOTE_API_URL).then(res => res.json()) // Fetch quote from ZenQuotes API
1515
]);
1616

17-
// Assuming the Vercel API responds with an imageUrl field
18-
const fullImageUrl = imageData.imageUrl;
19-
const quote = quoteData.content;
20-
const author = quoteData.author;
17+
console.log("Fetched image data:", imageData);
18+
console.log("Fetched quote data:", quoteData);
19+
20+
// Check if imageData is valid
21+
const fullImageUrl = imageData?.imageUrl || 'https://via.placeholder.com/1920x1080'; // Fallback image
22+
23+
// Access quote and author from the new response structure
24+
const quote = quoteData?.quote || "No quote available"; // Fallback quote
25+
const author = quoteData?.author || "Unknown"; // Fallback author
2126

2227
// Store the fetched image and quote in Chrome storage
2328
chrome.storage.local.set({

src/content.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ document.addEventListener("DOMContentLoaded", function () {
3939

4040
// Fetch and store a new image
4141
async function fetchImage() {
42-
const UNSPLASH_API_URL = "https://motivision.vercel.app/api/handler.js"; // Updated URL to your Vercel-hosted API
42+
const UNSPLASH_API_URL = "https://motivision.vercel.app/api/handler.js"; // Vercel-hosted API for image
4343

4444
console.log("Fetching new image...");
4545
try {
@@ -53,15 +53,17 @@ document.addEventListener("DOMContentLoaded", function () {
5353
}
5454
}
5555

56-
// Fetch and store a new quote
56+
// Fetch and store a new quote from ZenQuotes
5757
async function fetchQuote() {
58-
const QUOTE_API_URL = "https://motivision.vercel.app/api/handlerq.js";
58+
const QUOTE_API_URL = "https://motivision.vercel.app/api/handlerq.js"; // Your ZenQuotes API URL
5959

6060
console.log("Fetching new quote...");
6161
try {
6262
const quoteData = await fetch(QUOTE_API_URL).then(res => res.json());
63-
const newQuote = quoteData.content;
64-
const newAuthor = quoteData.author;
63+
64+
// Directly access quote and author from the response
65+
const newQuote = quoteData.quote; // Quote text
66+
const newAuthor = quoteData.author; // Author
6567

6668
chrome.storage.local.set({
6769
quote: newQuote,

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</div>
1616

1717
<div id="credits">
18-
<p id="unsplash-credit">Background Image from <a href="https://unsplash.com" target="_blank">Unsplash</a> and Quotes by <a href="https://github.com/lukePeavey/quotable" target="_blank">Quotable</a></p>
18+
<p id="unsplash-credit">Background Image from <a href="https://unsplash.com" target="_blank">Unsplash</a> and Quotes by <a href="https://zenquotes.io/" target="_blank">ZenQuotes</a></p>
1919
</div>
2020

2121
<p id="made-by">Made with ❤️ by <span style="font-weight: bold;">Kxvin</span></p>

vercel.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
{
99
"src": "api/handler.js",
1010
"use": "@vercel/node"
11+
},
12+
{
13+
"src": "api/handlerq.js",
14+
"use": "@vercel/node"
1115
}
1216
],
1317
"outputDirectory": "dist"

0 commit comments

Comments
 (0)