Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bring back interactive in-page popup for webextension #1084

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
"runner": "nx-cloud",
"options": {
"cacheableOperations": ["build", "test", "lint", "typecheck"],
"accessToken": "OGY4NDE3OTItYWViMS00YWM0LTk4ODgtYmI2ZWNhYjY1OGMyfHJlYWQtb25seQ=="
"accessToken": "OGY4NDE3OTItYWViMS00YWM0LTk4ODgtYmI2ZWNhYjY1OGMyfHJlYWQtb25seQ==",
"useDaemonProcess": false
}
}
},
"extends": "nx/presets/npm.json",
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"targetDefaults": {
"build": {
"inputs": ["default"],
"dependsOn": ["^build"],
"outputs": ["{projectRoot}/dist", "{projectRoot}/www"]
},
Expand Down
21 changes: 13 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"https-proxy-agent": "^5.0.1",
"ical-generator": "^3.6.0",
"joi": "^17.7.0",
"jsdom": "^21.1.0",
"jsdom": "^22.1.0",
"linkify-string": "^4.1.0",
"linkifyjs": "^4.1.0",
"mdb": "git+https://[email protected]/julianpoy/node-mdb.git",
Expand Down
11 changes: 11 additions & 0 deletions packages/webextension-v3/src/action/action.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@
</head>
<body>
<div class="container">
<div id="start">
<div class="headline">
<img class="logo" /><br /><br />
<h2>Import Site</h2>
</div>
<span id="message">
<button id="auto-import">&nbsp;Auto Import&nbsp;</button>&nbsp;
<button id="interactive-import">&nbsp;Interactive Import&nbsp;</button
>&nbsp;
</span>
</div>
<div id="importing">
<div class="headline">
<img class="logo" /><br /><br />
Expand Down
136 changes: 90 additions & 46 deletions packages/webextension-v3/src/action/action.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const API_BASE = "https://api.recipesage.com/";

chrome.runtime.onMessage.addListener((request) => {
const clipData = request;
console.log(clipData);

saveClip(clipData);
});

let token;

const login = async () => {
Expand Down Expand Up @@ -44,7 +51,7 @@ const login = async () => {
window.close();
}, 2000);
} else {
tutorial();
showTutorial();
}
});
});
Expand All @@ -54,31 +61,40 @@ const login = async () => {
}
};

const tutorial = () => {
const showTutorial = () => {
document.getElementById("login").style.display = "none";
document.getElementById("tutorial").style.display = "block";
document.getElementById("importing").style.display = "none";
document.getElementById("start").style.display = "none";
};

const loading = () => {
const showLoading = () => {
document.getElementsByTagName("html")[0].style.display = "initial";
document.getElementById("login").style.display = "none";
document.getElementById("tutorial").style.display = "none";
document.getElementById("importing").style.display = "block";
document.getElementById("start").style.display = "none";
};

const launch = (token) => {
newClip(token);
const showStart = () => {
document.getElementsByTagName("html")[0].style.display = "initial";
document.getElementById("login").style.display = "none";
document.getElementById("tutorial").style.display = "none";
document.getElementById("importing").style.display = "none";
document.getElementById("start").style.display = "block";
};

const fetchAndCreateImage = async (imageURL) => {
const imageBlobResponse = await fetch(imageURL);

if (!imageBlobResponse.ok) return;
const showLogin = () => {
document.getElementsByTagName("html")[0].style.display = "initial";
document.getElementById("login").style.display = "block";
document.getElementById("tutorial").style.display = "none";
document.getElementById("importing").style.display = "none";
document.getElementById("start").style.display = "none";
};

const imageBlob = await imageBlobResponse.blob();
const createImageFromBlob = async (imageBlob) => {
const formData = new FormData();
formData.append("image", imageBlob, "image");
formData.append("image", imageBlob);

const imageCreateResponse = await fetch(`${API_BASE}images?token=${token}`, {
method: "POST",
Expand All @@ -92,22 +108,47 @@ const fetchAndCreateImage = async (imageURL) => {
return imageData.id;
};

const newClip = async () => {
loading();

const interactiveClip = async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

let result;
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["/inject/inject.js"],
});

window.close();
};

const autoClip = async () => {
showLoading();

try {
[{ result }] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => document.documentElement.innerHTML,
});
await clipWithInject();
} catch (e) {
console.error(e);
window.alert("Failed to fetch page content");
return;
try {
await clipWithAPI();
} catch (e) {
window.alert("Failed to fetch page content");
}
}
};

const clipWithInject = async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["/inject/clip.js"],
});
};

const clipWithAPI = async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

const [{ result }] = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: () => document.documentElement.innerHTML,
});

const clipResponse = await fetch(`${API_BASE}clip?token=${token}`, {
method: "POST",
Expand All @@ -129,9 +170,20 @@ const newClip = async () => {

const clipData = await clipResponse.json();

const imageId = clipData.imageURL?.trim()
? await fetchAndCreateImage(clipData.imageURL)
: undefined;
await saveClip(clipData);
};

const saveClip = async (clipData) => {
let imageId;
if (clipData.imageBase64) {
try {
const response = await fetch(clipData.imageBase64);
const blob = await response.blob();
imageId = await createImageFromBlob(blob);
} catch (e) {
console.error(e);
}
}

const recipeCreateResponse = await fetch(
`${API_BASE}recipes?token=${token}`,
Expand Down Expand Up @@ -180,20 +232,6 @@ const newClip = async () => {
}, 500);
};

const tokenFetchPromise = new Promise((resolve, reject) => {
chrome.storage.local.get(["token"], (result) => {
token = result.token;

// If user is logged in, launch the tool
if (token) {
launch(token);
reject();
} else {
resolve();
}
});
});

document.addEventListener("DOMContentLoaded", () => {
[...document.getElementsByClassName("logo")].forEach(
(logo) =>
Expand All @@ -206,12 +244,18 @@ document.addEventListener("DOMContentLoaded", () => {
if (event.key === "Enter") login();
};
document.getElementById("tutorial-submit").onclick = () => window.close();
document.getElementById("auto-import").onclick = autoClip;
document.getElementById("interactive-import").onclick = interactiveClip;

tokenFetchPromise
.then(() => {
document.getElementsByTagName("html")[0].style.display = "initial";
})
.catch(() => {
// Do nothing
});
chrome.storage.local.get(["token"], (result) => {
token = result.token;

document.getElementsByTagName("html")[0].style.display = "initial";

if (token) {
showStart();
} else {
showLogin();
}
});
});
41 changes: 41 additions & 0 deletions packages/webextension-v3/src/inject/alert.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
:host {
all: initial !important;
contain: content;
}

.alert {
position: fixed;
bottom: 10px;
right: 10px;
width: 300px;
padding: 20px;

z-index: 99999999999999999999999999;
background: white;
box-shadow: 1px 1px 12px #666666;

user-select: none;

font-size: 14px;

font-family: Arial, Helvetica, sans-serif;

color: black;
}

.headline {
display: flex;
align-items: center;
margin-bottom: 15px;
}

img {
width: 30px;
height: 30px;
border-radius: 20px;
}

h3 {
margin: 0 10px;
font-size: 16px;
}
Loading