Skip to content

Commit 3930883

Browse files
committed
Rewrote to get current web page HTML, prompt to confirm within the extension, and send everything to server.
1 parent d5eee04 commit 3930883

File tree

7 files changed

+264
-22
lines changed

7 files changed

+264
-22
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Micro.blog
3+
Copyright (c) 2025 Micro.blog
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

background.js

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,45 @@
1-
function saveBookmark(tab) {
2-
// browser.tabs.query({ active: true, currentWindow: true }, function(tabs) {
3-
// let tab = tabs[0];
4-
// let url = tab.url;
5-
//
6-
// browser.tabs.create({
7-
// url: "https://micro.blog/bookmarks?go=" + encodeURIComponent(url)
8-
// });
9-
// });
10-
11-
let url= tab.url;
12-
chrome.tabs.create({
13-
url: "https://micro.blog/bookmarks?go=" + encodeURIComponent(url)
14-
});
15-
}
1+
// on toolbar click, inject content‐script into the active tab
2+
chrome.action.onClicked.addListener(tab => {
3+
chrome.scripting.executeScript({
4+
target: { tabId: tab.id },
5+
files: ['content-script.js']
6+
});
7+
});
168

17-
// browser.browserAction.onClicked.addListener(saveBookmark);
9+
// listen for messages from content‐script
10+
chrome.runtime.onMessage.addListener((msg, sender, completion_handler) => {
11+
if (msg.type == 'SAVE_BOOKMARK') {
12+
// store data in chrome.storage.local with a unique key
13+
const bookmark_key = 'bookmark_' + Date.now();
14+
chrome.storage.local.set({ [bookmark_key]: msg.data }, () => {
15+
const url = chrome.runtime.getURL('prompt.html') + '?key=' + bookmark_key;
16+
chrome.tabs.create({ url: url });
17+
});
18+
return;
19+
}
20+
else if (msg.type == 'CONFIRM_BOOKMARK') {
21+
const form = new FormData();
22+
form.append('bookmark-of', msg.data.url);
23+
form.append('bookmark-name', msg.data.title);
24+
form.append('bookmark-content', msg.data.html);
1825

19-
chrome.action.onClicked.addListener((tab) => {
20-
saveBookmark(tab);
21-
});
26+
// POST to Micro.blog
27+
fetch('https://stoic_neumann.orb.local/micropub', {
28+
method: 'POST',
29+
body: form
30+
})
31+
.then(response => {
32+
if (!response.ok) {
33+
throw new Error(response.statusText);
34+
}
35+
completion_handler({ success: true });
36+
})
37+
.catch(err => {
38+
console.error('Error saving bookmark:', err);
39+
completion_handler({ success: false, error: err.message });
40+
});
41+
42+
// keep the channel open for the async handler
43+
return true;
44+
}
45+
});

content-script.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
(() => {
2+
const message = {
3+
type: 'SAVE_BOOKMARK',
4+
data: {
5+
url: location.href,
6+
title: document.title,
7+
html: document.documentElement.outerHTML
8+
}
9+
};
10+
11+
chrome.runtime.sendMessage(message, response => {
12+
if (response && response.success) {
13+
console.log('Bookmark saved successfully');
14+
}
15+
else {
16+
console.error('Failed to save bookmark', response && response.error);
17+
}
18+
});
19+
})();

images/spinner.svg

Lines changed: 6 additions & 0 deletions
Loading

manifest.json

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,29 @@
22
"name": "Micro.blog",
33
"description": "Save bookmarks to Micro.blog.",
44
"manifest_version": 3,
5-
"version": "1.0.1",
6-
"permissions": [ "activeTab" ],
5+
"version": "1.1",
6+
"permissions": [
7+
"tabs",
8+
"scripting",
9+
"storage"
10+
],
11+
"host_permissions": [
12+
"<all_urls>"
13+
],
714
"background": {
815
"service_worker": "background.js"
916
},
17+
"web_accessible_resources": [
18+
{
19+
"resources": [
20+
"prompt.html",
21+
"prompt.js",
22+
"icons/icon-64.png",
23+
"images/spinner.svg"
24+
],
25+
"matches": [ "<all_urls>" ]
26+
}
27+
],
1028
"icons": {
1129
"16": "icons/icon-16.png",
1230
"32": "icons/icon-32.png",

prompt.html

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Micro.blog: Save Bookmark</title>
6+
<style>
7+
body {
8+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
9+
margin: 0;
10+
background: #fff;
11+
}
12+
13+
.container {
14+
max-width: 600px;
15+
margin: 40px auto 0 auto;
16+
padding: 0 24px;
17+
}
18+
19+
.header {
20+
display: flex;
21+
align-items: center;
22+
margin-bottom: 36px;
23+
}
24+
25+
.header img {
26+
width: 32px;
27+
height: 32px;
28+
margin-right: 8px;
29+
}
30+
31+
.header span {
32+
font-size: 1.3rem;
33+
font-weight: 500;
34+
color: #ff9900;
35+
}
36+
37+
.save-label {
38+
font-size: 1.1rem;
39+
margin-bottom: 22px;
40+
margin-top: 16px;
41+
}
42+
43+
.bookmark-url {
44+
font-size: 1rem;
45+
font-weight: bold;
46+
margin-bottom: 32px;
47+
}
48+
49+
.save-btn {
50+
font-size: 1rem;
51+
padding: 8px 14px;
52+
border-radius: 7px;
53+
border: 1px solid #d4d4d4;
54+
background: #f8f8f8;
55+
cursor: pointer;
56+
}
57+
58+
.save-btn:hover {
59+
background: #eaeaea;
60+
}
61+
62+
.spinner {
63+
display: none;
64+
width: 30px;
65+
height: 30px;
66+
vertical-align: middle;
67+
margin-left: 5px;
68+
margin-bottom: 5px;
69+
}
70+
71+
@media (prefers-color-scheme: dark) {
72+
body {
73+
background: #111;
74+
color: #eee;
75+
}
76+
77+
.header span {
78+
color: #ffb84d;
79+
}
80+
81+
.save-label {
82+
color: #ccc;
83+
}
84+
85+
.bookmark-url {
86+
color: #fff;
87+
}
88+
89+
.save-btn {
90+
background: #222;
91+
border-color: #444;
92+
color: #eee;
93+
}
94+
95+
.save-btn:hover {
96+
background: #333;
97+
}
98+
}
99+
</style>
100+
</head>
101+
<body>
102+
<div class="container">
103+
<div class="header">
104+
<img src="icons/icon-64.png" alt="Micro.blog icon">
105+
<span>Micro.blog</span>
106+
</div>
107+
<div class="save-label">Save bookmark for:</div>
108+
<div class="bookmark-url" id="bookmark-url"></div>
109+
110+
<form id="bookmark-form">
111+
<input type="hidden" id="title" name="title">
112+
<input type="hidden" id="url" name="url">
113+
<input type="hidden" id="html" name="html">
114+
<button type="submit" class="save-btn" id="save-btn">Save</button>
115+
<img src="images/spinner.svg" alt="Progress spinner" id="spinner" class="spinner">
116+
</form>
117+
<script src="prompt.js"></script>
118+
</div>
119+
</body>
120+
</html>

prompt.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
window.onload = () => {
2+
const params = new URLSearchParams(window.location.search);
3+
const bookmark_key = params.get('key');
4+
if (!bookmark_key) {
5+
return;
6+
}
7+
8+
chrome.storage.local.get([bookmark_key], items => {
9+
const data = items[bookmark_key] || {};
10+
11+
// clean URL for display
12+
let display_url = (data.url || '').replace(/^https?:\/\//, '');
13+
if (display_url.length > 100) {
14+
display_url = display_url.slice(0, 100) + '…';
15+
}
16+
17+
// show display URL
18+
const url_div = document.getElementById('bookmark-url');
19+
if (url_div) {
20+
url_div.textContent = display_url;
21+
}
22+
23+
// populate hidden fields
24+
document.getElementById('title').value = data.title || '';
25+
document.getElementById('url').value = data.url || '';
26+
document.getElementById('html').value = data.html || '';
27+
28+
// clear storage after loading
29+
chrome.storage.local.remove(bookmark_key);
30+
});
31+
32+
document.getElementById('bookmark-form').onsubmit = function(e) {
33+
e.preventDefault();
34+
35+
const save_button = document.getElementById('save-btn');
36+
const spinner = document.getElementById('spinner');
37+
save_button.disabled = true;
38+
spinner.style.display = 'inline-block';
39+
40+
const bookmark = {
41+
url: document.getElementById('url').value,
42+
title: document.getElementById('title').value,
43+
html: document.getElementById('html').value
44+
};
45+
chrome.runtime.sendMessage({
46+
type: 'CONFIRM_BOOKMARK',
47+
data: bookmark
48+
}, () => {
49+
// wait a half second before closing
50+
setTimeout(() => {
51+
window.close();
52+
}, 500);
53+
});
54+
};
55+
};

0 commit comments

Comments
 (0)