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
+ } ) ;
16
8
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 ) ;
18
25
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
+ } ) ;
0 commit comments