Skip to content

Commit 83c2155

Browse files
committed
working version
1 parent ed697da commit 83c2155

File tree

6 files changed

+129
-38
lines changed

6 files changed

+129
-38
lines changed

browser-extensions/background.js

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,79 @@
1-
chrome.action.onClicked.addListener((tab) => {
2-
chrome.scripting.executeScript({
3-
target: { tabId: tab.id },
4-
files: ['content.js']
5-
});
6-
});
7-
81
chrome.runtime.onMessage.addListener(
92
function (request, sender, sendResponse) {
103
if (request.contentScriptQuery == "parseRecipe") {
11-
12-
const data = new FormData();
13-
data.append('file', null); // TODO: html file goes here
14-
data.append('url', request.url);
15-
data.append('downloadImage', request.downloadImage);
16-
17-
// let postBody = { "url": request.url, "downloadImage": request.downloadImage };
18-
let options = {
19-
method: "POST",
20-
body: data
21-
};
22-
23-
fetch(`https://sharpcooking.lpains.net/api/parse-recipe-html`, options)
24-
.then(response => {
25-
if (response.ok) {
26-
return response.json();
27-
} else {
28-
throw new Error(`Something went wrong: ${response.status}`);
29-
}
30-
})
31-
.then(data => {
32-
console.log(data);
33-
sendResponse(data);
34-
})
35-
.catch(error => {
36-
console.log(error);
37-
});
4+
parseRecipe(request.url, request.downloadImage, request.body, sendResponse);
385

396
return true; // Will respond asynchronously.
407
}
41-
});
8+
});
9+
10+
function parseRecipe(url, downloadImage, body, sendResponse) {
11+
const postFile = new Blob([body], {
12+
type: 'application/x-object'
13+
});
14+
15+
const data = new FormData();
16+
data.append('file', postFile);
17+
data.append('url', url);
18+
data.append('downloadImage', downloadImage);
19+
20+
let options = {
21+
method: "POST",
22+
body: data
23+
};
24+
25+
fetch(`https://delightful-flower-0c3edd710-383.centralus.2.azurestaticapps.net/api/parse-recipe-html`, options)
26+
.then(response => {
27+
if (response.ok) {
28+
return response.json();
29+
} else {
30+
throw new Error(`Something went wrong: ${response.status}`);
31+
}
32+
})
33+
.then(data => {
34+
shareParsedRecipe(data, sendResponse);
35+
})
36+
.catch(error => {
37+
chrome.runtime.sendMessage(
38+
{ contentScriptQuery: "parseRecipeResult", error: error },
39+
);
40+
console.log(error);
41+
});
42+
}
43+
44+
function shareParsedRecipe(parseResult, sendResponse) {
45+
const body = {
46+
title: parseResult.title,
47+
ingredients: parseResult.ingredients.map(item => item.raw),
48+
steps: parseResult.steps.map(item => item.raw),
49+
notes: "",
50+
source: parseResult.host ?? "imported from browser",
51+
media: []
52+
};
53+
let options = {
54+
method: "POST",
55+
headers: { 'content-type': 'application/json' },
56+
body: JSON.stringify(body)
57+
};
58+
59+
fetch(`https://delightful-flower-0c3edd710-383.centralus.2.azurestaticapps.net/api/share-recipe`, options)
60+
.then(response => {
61+
if (response.ok) {
62+
return response.json();
63+
} else {
64+
throw new Error(`Something went wrong: ${response.status}`);
65+
}
66+
})
67+
.then(data => {
68+
sendResponse(data.id)
69+
chrome.runtime.sendMessage(
70+
{ contentScriptQuery: "parseRecipeResult", code: data.id },
71+
);
72+
})
73+
.catch(error => {
74+
chrome.runtime.sendMessage(
75+
{ contentScriptQuery: "parseRecipeResult", error: error },
76+
);
77+
console.log(error);
78+
});
79+
}

browser-extensions/content.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
chrome.runtime.sendMessage(
2-
{ contentScriptQuery: "parseRecipe", url: document.URL, downloadImage: false },
2+
{ contentScriptQuery: "parseRecipe", url: document.URL, downloadImage: false, body: document.documentElement.outerHTML },
33
response => console.log(response));

browser-extensions/manifest.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
"activeTab",
99
"scripting"
1010
],
11-
"action": {},
11+
"action": {
12+
"default_popup": "popup.html"
13+
},
1214
"background": {
1315
"service_worker": "background.js"
1416
},
15-
"host_permissions": ["https://sharpcooking.lpains.net/api/*"]
17+
"host_permissions": [
18+
"https://sharpcooking.lpains.net/api/*",
19+
"https://delightful-flower-0c3edd710-383.centralus.2.azurestaticapps.net/api/*"
20+
]
1621
}

browser-extensions/popup.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<html>
2+
3+
<head>
4+
<style>
5+
.container {
6+
min-height: 5rem;
7+
width: 10rem;
8+
padding: 1rem;
9+
}
10+
.result {
11+
font-size: 2rem;
12+
margin: auto;
13+
}
14+
</style>
15+
</head>
16+
17+
<body>
18+
<div class="container">
19+
<div id="sharp-cooking-message">Trying to extract recipe. Please wait...</div>
20+
<div id="sharp-cooking-code" class="result"></div>
21+
</div>
22+
<script src="popup.js">
23+
</script>
24+
</body>
25+
26+
</html>

browser-extensions/popup.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
2+
chrome.scripting.executeScript({
3+
target: { tabId: tabs[0].id },
4+
files: ['content.js']
5+
});
6+
});
7+
8+
chrome.runtime.onMessage.addListener(
9+
function (request) {
10+
if (request.contentScriptQuery == "parseRecipeResult") {
11+
document.getElementById("sharp-cooking-code").innerHTML = request.code || "";
12+
document.getElementById("sharp-cooking-message").innerHTML = request.error || "";
13+
14+
return false; // there is no need to respond
15+
}
16+
}
17+
);

src/pages/recipe/[id]/edit.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ async function importRecipeFromCode() {
386386
});
387387
388388
item.value.imageAvailable = images.value.length > 0;
389+
390+
if (editInSingleTextArea.value) {
391+
ingredientsText.value = item.value.ingredients.join("\n");
392+
stepsText.value = item.value.steps.join("\n");
393+
}
389394
}
390395
catch {
391396
isImportFromUrlModalOpen.value = true;

0 commit comments

Comments
 (0)