Skip to content

Right click image uploads #9

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"@emotion/css": "^11.1.3",
"@emotion/react": "^11.4.1",
"@fontsource/inter": "^4.5.0",
"dotenv": "^10.0.0",
"File": "^0.10.2",
"html2canvas": "^1.3.2",
"http2": "^3.3.7",
"jquery": "3.4.1",
"jquery": "^3.6.0",
"node-fetch": "^2.6.1",
"open-graph": "^0.2.6",
"prettier": "^2.4.0",
Expand Down Expand Up @@ -50,6 +51,6 @@
]
},
"devDependencies": {
"cross-env": "7.0.2"
"cross-env": "^7.0.3"
}
}
137 changes: 119 additions & 18 deletions public/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ const domains = {
cookie: 'https://slate.host'
}

function randomID() {
return Math.random().toString(36).substr(2, 9);
}

const getSessionID = async () => {
return new Promise((resolve, reject) => {
chrome.cookies.get({"url": domain, "name": "WEB_SERVICE_SESSION_KEY"}, cookie => {
Expand Down Expand Up @@ -63,7 +67,7 @@ const getUser = async (props) => {

const json = await response.json();
if (json.error) {
console.log(json);
console.log("User data: ", json);
} else {
const collections = json.collections;
const user = json.user;
Expand Down Expand Up @@ -91,7 +95,6 @@ const checkLink = async (props) => {
}

const handleSaveLink = async (props) => {

const apiKey = await getApiKey();
const response = await fetch(`${domain}/api/v2/create-link`, {
method: "POST",
Expand All @@ -107,30 +110,34 @@ const handleSaveLink = async (props) => {
});

const json = await response.json();
console.log('upload data: ', json)


console.log('Link upload: ', json)

if(json.decorator === "LINK_DUPLICATE") {
chrome.tabs.sendMessage(parseInt(props.tab), {
run: 'UPLOAD_DUPLICATE',
data: json.data[0]
data: json.data[0],
id: props.id
});
return;
}

if(json.decorator === "SERVER_CREATE_LINK_FAILED" || json.error === true) {
chrome.tabs.sendMessage(parseInt(props.tab), {
run: 'UPLOAD_FAIL',
id: props.id
});
return;
}

if(!props.background) {
chrome.tabs.sendMessage(parseInt(props.tab), {
run: 'UPLOAD_DONE',
data: json.data[0],
data: json.data[0],
id: props.id,
tab: props.tab
});
}else{
} else {
//If background upload, dont send a message to a tab
return;
}
Expand All @@ -151,7 +158,7 @@ handleSaveImage = async (props) => {
body: JSON.stringify({
data: {
url: props.url,
filename: props.url,
filename: props.url
},
}),
});
Expand Down Expand Up @@ -196,16 +203,16 @@ const checkLoginSession = async (tab) => {
}
}

chrome.action.onClicked.addListener(async (tab) => {
chrome.tabs.sendMessage(tab.id, { run: 'LOAD_APP', type: 'LOADER_MAIN', tabId: tab.id });
const openApp = async (tab) => {
chrome.tabs.sendMessage(parseInt(tab.id), { run: 'LOAD_APP', type: 'LOADER_MAIN' });
await checkLoginData(tab);
});
}

chrome.action.onClicked.addListener(async (tab) => { openApp(tab) });

chrome.commands.onCommand.addListener(async (command, tab) => {
if(command == 'open-app') {
chrome.tabs.sendMessage(tab.id, { run: 'LOAD_APP', type: 'LOADER_MAIN' });
await checkLoginData(tab);
}
if(command == 'open-app') openApp(tab);

if(command == 'open-slate') {
chrome.tabs.create({ 'url': `${domain}/_/data&extension=true&id=${tab.id}` });
}
Expand All @@ -227,9 +234,15 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
}

if(request.type === "SAVE_LINK") {
chrome.tabs.sendMessage(parseInt(sender.tab.id), { run: "OPEN_LOADING" });
let data = await handleSaveLink({ url: sender.url, tab: sender.tab.id })
}
let uploadId = randomID();
chrome.tabs.sendMessage(parseInt(sender.tab.id), {
run: "OPEN_LOADING",
id: uploadId,
image: sender.tab.favIconUrl,
title: sender.tab.title
});
let data = await handleSaveLink({ id: uploadId, url: sender.url, tab: sender.tab.id })
}

if(request.type === "CHECK_LOGIN") {
await checkLoginSession(sender.tab);
Expand Down Expand Up @@ -263,3 +276,91 @@ chrome.tabs.onUpdated.addListener(async (tabId , info , tab) => {
}
}
});

handleUploadImage = async (info, tab) => {
chrome.tabs.sendMessage(parseInt(tab.id), { run: 'LOAD_APP_RIGHT_CLICK', type: 'LOADER_MINI' });
let session = await checkLoginData(tab);

if(session.user) {

let uploadId = randomID();

const filename = info.srcUrl.replace(/^.*[\\\/]/, '')
const finalTitle = filename.split("?")[0];

chrome.tabs.sendMessage(parseInt(tab.id), {
run: 'OPEN_LOADING',
image: info.srcUrl,
id: uploadId,
title: finalTitle,
});

const apiKey = await getApiKey();
const url = 'https://uploads.slate.host/api/v2/public/upload-by-url';

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': "application/json",
Authorization: apiKey,
},
body: JSON.stringify({
data: {
url: info.srcUrl,
filename: finalTitle,
},
}),
});

const json = await response.json();
console.log('Image upload: ', json)

if(json.decorator === "LINK_DUPLICATE") {
chrome.tabs.sendMessage(parseInt(tab.id), {
run: 'UPLOAD_DONE',
id: uploadId,
data: json.data,
tab: tab
});
return;
}

if(json.decorator === "SERVER_CREATE_LINK_FAILED" || json.error === true) {
chrome.tabs.sendMessage(parseInt(tab.id), {
run: 'UPLOAD_FAIL',
id: uploadId,
data: json,
tab: tab
});
return;
}

chrome.tabs.sendMessage(parseInt(tab.id), {
run: 'UPLOAD_DONE',
id: uploadId,
data: json.data,
tab: tab
});
}

return;
}

handleMenuOpen = async (info, tab) => { await openApp(tab) }

chrome.contextMenus.create({
id: "image_slate",
title: "Save image",
contexts: ["image"],
}, () => chrome.runtime.lastError);

chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId == "parent") {
await handleMenuOpen(info, tab);
}

if (info.menuItemId == "image_slate") {
await handleUploadImage(info, tab)
}
});

49 changes: 39 additions & 10 deletions public/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ if(window.location.href.startsWith('https://slate.host')) {

let isJumperOpen = false;

chrome.runtime.onMessage.addListener(function(request, sender, callback) {
chrome.runtime.onMessage.addListener(async function(request, sender, callback) {

if(request.run === "LOAD_APP") {
if(!isJumperOpen) {
main({ type: request.type });
isJumperOpen = true;
return;
}
window.postMessage({ type: "SHOW_APP" }, "*");
return;
}

if(request.run === "LOAD_APP_RIGHT_CLICK") {
if(!isJumperOpen) {
main({ type: request.type });
isJumperOpen = true;
return;
}
}

if(request.run === "AUTH_REQ") {
window.postMessage({ type: "AUTH_REQ" }, "*");
return true;
Expand All @@ -38,22 +47,42 @@ chrome.runtime.onMessage.addListener(function(request, sender, callback) {
}

if(request.run === "OPEN_LOADING") {
window.postMessage({ type: "OPEN_LOADING" }, "*");
window.postMessage({
type: "OPEN_LOADING",
id: request.id,
image: request.image,
title: request.title,
}, "*");
return true;
}

if(request.run === "UPLOAD_DONE") {
window.postMessage({ type: "UPLOAD_DONE", data: request.data, tab: request.tab }, "*");
window.postMessage({
type: "UPLOAD_DONE",
id: request.id,
data: request.data,
tab: request.tab
}, "*");
return true;
}

if(request.run === "UPLOAD_FAIL") {
window.postMessage({ type: "UPLOAD_FAIL" }, "*");
window.postMessage({
type: "UPLOAD_FAIL",
id: request.id,
data: request.data,
tab: request.tab
}, "*");
return true;
}

if(request.run === "UPLOAD_DUPLICATE") {
window.postMessage({ type: "UPLOAD_DUPLICATE", data: request.data }, "*");
window.postMessage({
type: "UPLOAD_DUPLICATE",
id: request.id,
data: request.data,
tab: request.tab
}, "*");
return true;
}

Expand Down Expand Up @@ -101,7 +130,11 @@ window.addEventListener("message", async function(event) {
}

if(event.data.run === "OPEN_LOADING") {
chrome.runtime.sendMessage({ type: "SAVE_LINK", url: event.data.url });
chrome.runtime.sendMessage({
type: "SAVE_LINK",
url: event.data.url,
image: event.data.image,
});
return true;
}

Expand All @@ -119,9 +152,5 @@ window.addEventListener("message", async function(event) {
chrome.runtime.sendMessage({ type: "SIGN_OUT" });
}

if(event.data.run === "SET_OPEN_FALSE") {
isJumperOpen = false;
}

if (event.source !== window) return;
});
11 changes: 6 additions & 5 deletions public/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"matches": ["<all_urls>"],
"all_frames": false,
"js": [
"./jquery.js",
"./content.js"
"./content.js",
"./jquery.js"
],
"run_at": "document_idle"
}],
Expand All @@ -31,7 +31,7 @@
"cookies",
"commands"
],
"host_permissions": ["https://slate.host/"],
"host_permissions": ["https://slate.host/", "http://*/*", "https://*/*"],
"commands": {
"open-app": {
"suggested_key": {
Expand Down Expand Up @@ -65,9 +65,10 @@
"/static/*",
"/fonts/*",
"Inter-Regular.ttf",
"../fonts/*"
"../fonts/*",
"icon128.png"
],
"matches": ["<all_urls>"]
"matches": ["<all_urls>", "http://*/*", "https://*/*"]
}
]
}
Loading