diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/background.js b/background.js old mode 100644 new mode 100755 index 04bbb1d..d0c3f5b --- a/background.js +++ b/background.js @@ -5,15 +5,15 @@ const ADDING_TEXT = "WAIT"; const INIT_TEXT = "LOAD"; const REMOVING_TEXT = "WAIT"; const ERROR_TEXT = "ERR"; -const GRAPHQL_ENDPOINT = "https://2887df8.commit.truffle.tools/api/graphql" +const GRAPHQL_ENDPOINT = "https://truffle.tools/api/graphql" async function isBookmarked(repoName) { const keys = await chrome.storage.sync.get(['apiKey', 'userApiKey']) - if(!keys.apiKey || keys.apiKey.length < 36) { + if (!keys.apiKey || keys.apiKey.length < 36) { throw new Error("No api key"); } - if(!keys.userApiKey || keys.userApiKey.length != 36) { + if (!keys.userApiKey || keys.userApiKey.length != 36) { throw new Error("No user api key"); } @@ -24,7 +24,8 @@ async function isBookmarked(repoName) { headers: { "Content-Type": "application/json", "apiKey": keys.apiKey, - "userapikey": keys.userApiKey + "userapikey": keys.userApiKey, + "x-server": 'supabase' }, body: JSON.stringify({ query: `query { @@ -34,21 +35,32 @@ async function isBookmarked(repoName) { }); const result = await response.json() - + return !!result?.data?.fIsGthbRepoBookmarked; } async function init(tab) { - if (tab.url.startsWith(githubUrl) && tab.url.substring(githubUrl.length).length > 0 && tab.url.substring(githubUrl.length).includes('/')) { + console.log('one') + if (tab.url.startsWith(githubUrl) && tab.url.substring(githubUrl.length).length > 0 && tab.url.substring(githubUrl.length).includes('/')) { const urlPartsString = tab.url.substring(githubUrl.length); const urlParts = urlPartsString.split("/"); - if (urlParts.length > 2) + if (urlParts.length > 2) { + await chrome.action.setBadgeText({ + tabId: tab.id, + text: "", + }); return; + } const [org, repo] = urlParts - if (org.length === 0 || repo.length === 0) + if (org.length === 0 || repo.length === 0) { + await chrome.action.setBadgeText({ + tabId: tab.id, + text: "", + }); return; + } try { await chrome.action.setBadgeBackgroundColor({ color: [210, 210, 210, 255] }); await chrome.action.setBadgeText({ @@ -70,12 +82,12 @@ async function init(tab) { text: CLEAR_TEXT, }); } - } catch(error) { + } catch (error) { - if(error.message === "No api key" || error.message === "No user api key") { + if (error.message === "No api key" || error.message === "No user api key") { chrome.scripting.executeScript({ - target: {tabId: tab.id}, - function: function() { + target: { tabId: tab.id }, + function: function () { alert('truffle-ai-extension needs two api keys. Add it on the options page.'); } }); @@ -86,9 +98,9 @@ async function init(tab) { } else { // fail silently, maybe url was not a repo await chrome.action.setBadgeText({ - tabId: tab.id, - text: "", - }); + tabId: tab.id, + text: "", + }); } } } @@ -100,12 +112,11 @@ chrome.tabs.onActivated.addListener(function (activeInfo) { }); chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) { - const badgeText = await chrome.action.getBadgeText({ tabId: tab.id }); - - if(badgeText == "") { - + console.log(tabId, changeInfo, tab) + const badgeText = await chrome.action.getBadgeText({ tabId: tab.id }); + if (changeInfo.url) init(tab) - } + }); chrome.action.onClicked.addListener(async (tab) => { @@ -113,12 +124,12 @@ chrome.action.onClicked.addListener(async (tab) => { const apikey = await chrome.storage.sync.get('apikey') const urlPartsString = tab.url.substring(githubUrl.length); const urlParts = urlPartsString.split("/"); - if(urlParts.length > 2) + if (urlParts.length > 2) return; - const badgeText = await chrome.action.getBadgeText({ tabId: tab.id }); + const badgeText = await chrome.action.getBadgeText({ tabId: tab.id }); const [org, repo] = urlParts - if(org.length === 0 || repo.length === 0) + if (org.length === 0 || repo.length === 0) return; const hasBookmark = await isBookmarked(repo); @@ -132,16 +143,19 @@ chrome.action.onClicked.addListener(async (tab) => { text: ADDING_TEXT, }); - // add it - + // add it + const keys = await chrome.storage.sync.get(['apiKey', 'userApiKey']) + + console.log(keys) + const response = await fetch(GRAPHQL_ENDPOINT, { method: "POST", mode: "cors", cache: "no-cache", headers: { "Content-Type": "application/json", - "userapikey": apikey.apikey - + "userapikey": keys.userApiKey, + "x-server": 'server' }, body: JSON.stringify({ query: `mutation { @@ -149,10 +163,10 @@ chrome.action.onClicked.addListener(async (tab) => { }` }), }) - + const result = await response.json() - if(result?.data?.createBookmark === true) { + if (result?.data?.createBookmark === true) { await chrome.action.setBadgeBackgroundColor({ color: [0, 255, 0, 255] }); await chrome.action.setBadgeText({ tabId: tab.id, @@ -162,10 +176,10 @@ chrome.action.onClicked.addListener(async (tab) => { } else { throw new Error("Failed to add repo") } - } catch { + } catch { chrome.scripting.executeScript({ - target: {tabId: tab.id}, - function: function() { + target: { tabId: tab.id }, + function: function () { alert('truffle-ai-extension was unable to add the repo. Check if you added your user api key in the options page of the extension.'); } }); @@ -174,7 +188,7 @@ chrome.action.onClicked.addListener(async (tab) => { text: ERROR_TEXT, }); } - + } else { try { await chrome.action.setBadgeBackgroundColor({ color: [255, 199, 0, 255] }); @@ -182,14 +196,15 @@ chrome.action.onClicked.addListener(async (tab) => { tabId: tab.id, text: REMOVING_TEXT, }); - + const response = await fetch(GRAPHQL_ENDPOINT, { method: "POST", mode: "cors", cache: "no-cache", headers: { "Content-Type": "application/json", - "userapikey": apikey.apikey + "userapikey": apikey.apikey, + "x-server": 'supabase' }, body: JSON.stringify({ @@ -214,18 +229,18 @@ chrome.action.onClicked.addListener(async (tab) => { } } catch { chrome.scripting.executeScript({ - target: {tabId: tab.id}, - function: function() { + target: { tabId: tab.id }, + function: function () { alert('truffle-ai-extension was unable to remove the repo. Check if you added your user api key in the options page of the extension.'); } }); - + await chrome.action.setBadgeText({ tabId: tab.id, text: ERROR_TEXT, }); } - + } } }); diff --git a/images/icon-128.png b/images/icon-128.png old mode 100644 new mode 100755 diff --git a/images/icon-16.png b/images/icon-16.png old mode 100644 new mode 100755 diff --git a/images/icon-32.png b/images/icon-32.png old mode 100644 new mode 100755 diff --git a/images/icon-48.png b/images/icon-48.png old mode 100644 new mode 100755 diff --git a/manifest.json b/manifest.json old mode 100644 new mode 100755 diff --git a/options.html b/options.html old mode 100644 new mode 100755 diff --git a/options.js b/options.js old mode 100644 new mode 100755