A Firefox extension that saves the page you're on as a bookmark in your AskRobots account, in one click.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/bookmarks-firefox.git
HTTPS
https://gitstable.com/git/askrobots/bookmarks-firefox.git
SSH
ssh://git@gitstable.com:2222/askrobots/bookmarks-firefox.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

askrobots Update popup.js api endpoint changed. b68e3f0 2 years, 6 months ago History
popup.js · 2.8 KB · 77 lines
1// popup.js
2document.getElementById('openSettings').addEventListener('click', () => {
3 chrome.runtime.sendMessage({action: 'openOptionsPage'});
4});
5
6// popup.js
7document.addEventListener("DOMContentLoaded", async () => {
8 let [tab] = await browser.tabs.query({ active: true, currentWindow: true });
9 let url = tab.url;
10 let buttonElement = document.getElementById("saveBookmark");
11
12 try {
13 let { apiToken: token } = await browser.storage.local.get('apiToken');
14 const response = await fetch(`https://askrobots.com/api/check_link/?url=${encodeURIComponent(url)}`, {
15 method: 'GET',
16 headers: {
17 'Authorization': `Token ${token}`,
18 },
19 });
20
21 const data = await response.json();
22 if (data.bookmarked) {
23 buttonElement.style.backgroundColor = "green";
24 buttonElement.textContent = "Bookmark Exists!";
25 } else {
26 buttonElement.style.backgroundColor = "";
27 buttonElement.textContent = "Save Bookmark";
28 }
29 } catch (error) {
30 console.error(`Error: ${error.message}`);
31 }
32
33 buttonElement.addEventListener("click", saveBookmark); // Assuming saveBookmark is your function to save a bookmark
34});
35
36async function saveBookmark() {
37 const buttonElement = document.getElementById("saveBookmark");
38 console.log("Button clicked.");
39
40 let [tab] = await browser.tabs.query({ active: true, currentWindow: true });
41 let url = tab.url;
42 let title = tab.title;
43
44 console.log(`Attempting to bookmark: ${title} - ${url}`);
45
46 try {
47 let { apiToken: token } = await browser.storage.local.get('apiToken');
48 const response = await fetch('https://askrobots.com/api/links/', {
49 method: 'POST',
50 headers: {
51 'Authorization': `Token ${token}`,
52 'Content-Type': 'application/json',
53 },
54 body: JSON.stringify({
55 title: title,
56 url: url,
57 }),
58 });
59
60 const responseData = await response.json();
61
62 if (!response.ok) {
63 console.error(`Server responded with status: ${response.status}`);
64 buttonElement.style.backgroundColor = "red"; // Set button color to red on error
65 buttonElement.textContent = "Error!";
66 } else {
67 buttonElement.style.backgroundColor = "green"; // Set button color to green on success
68 buttonElement.textContent = "Bookmark Added!";
69 }
70
71 console.log(`Response: ${responseData}`);
72 } catch (error) {
73 console.error(`Error: ${error.message}`);
74 buttonElement.style.backgroundColor = "red"; // Set button color to red on exception
75 buttonElement.textContent = "Error!";
76 }
77}