Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pczern committed May 19, 2024
1 parent 9ef89a4 commit cb95b40
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 39 deletions.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
93 changes: 54 additions & 39 deletions background.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand All @@ -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 {
Expand All @@ -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({
Expand All @@ -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.');
}
});
Expand All @@ -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: "",
});
}
}
}
Expand All @@ -100,25 +112,24 @@ 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) => {
if (tab.url.startsWith(githubUrl) && tab.url.length > githubUrl.length && tab.url.substring(githubUrl.length).indexOf('/') !== -1) {
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);
Expand All @@ -132,27 +143,30 @@ 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 {
createBookmark(repo: {owner: "${org}", name: "${repo}"}, categories: ["chrome-ext"])
}`
}),
})


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,
Expand All @@ -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.');
}
});
Expand All @@ -174,22 +188,23 @@ chrome.action.onClicked.addListener(async (tab) => {
text: ERROR_TEXT,
});
}

} else {
try {
await chrome.action.setBadgeBackgroundColor({ color: [255, 199, 0, 255] });
await chrome.action.setBadgeText({
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({
Expand All @@ -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,
});
}

}
}
});
Empty file modified images/icon-128.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified images/icon-16.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified images/icon-32.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified images/icon-48.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified manifest.json
100644 → 100755
Empty file.
Empty file modified options.html
100644 → 100755
Empty file.
Empty file modified options.js
100644 → 100755
Empty file.

0 comments on commit cb95b40

Please sign in to comment.