paint-brush
Trình tạo thư mục từ dấu trang của trình duyệt: Cách thức hoạt độngby@bobnoxious
1,245
1,245

Trình tạo thư mục từ dấu trang của trình duyệt: Cách thức hoạt động

Bob Wright5m2023/11/27
Read on Terminal Reader

Trình tạo thư mục đơn giản sử dụng tệp Dấu trang của Trình duyệt làm nội dung nguồn. Bản thân cơ sở dữ liệu thực sự là một tệp đối tượng JSON mà chúng ta có thể lưu và sử dụng cục bộ. Ví dụ: điều này giúp tôi có cùng một bộ dấu trang trên Chrome, Edge và Firefox mà không cần trình duyệt lưu dữ liệu dấu trang của tôi. Chúng ta chỉnh sửa thư mục bookmark để trích xuất những bookmark duy nhất muốn sử dụng.
featured image - Trình tạo thư mục từ dấu trang của trình duyệt: Cách thức hoạt động
Bob Wright HackerNoon profile picture
0-item

Trình tạo thư mục đơn giản sử dụng tệp Dấu trang trình duyệt JSON làm nội dung nguồn.

Điều kiện tiên quyết và mã

Yêu cầu đầu tiên đối với hoạt động của trình tạo thư mục là nguồn dữ liệu nội dung của nó bao gồm đối tượng JSON được tạo bởi ứng dụng quản lý dấu trang trình duyệt có tên xbrowserSync . Ứng dụng này là một trình quản lý dấu trang bất khả tri gồm hai phần.


Một phần là tiện ích mở rộng của trình duyệt và phần còn lại là API máy chủ quản lý dữ liệu dấu trang dưới dạng tệp dữ liệu MongoDB . Tất cả những gì chúng ta cần biết về API máy chủ là URL của nó. Bạn có thể tìm thấy tiện ích mở rộng trình duyệt trong các “cửa hàng” ứng dụng trực tuyến như Google Play dành cho trình duyệt Chrome.


Ý tưởng cơ bản là ứng dụng xbrowserSync lưu trữ dữ liệu dấu trang của bạn trong cơ sở dữ liệu được mã hóa được lưu trữ trên máy chủ API. Bản thân cơ sở dữ liệu thực sự là một tệp đối tượng JSON mà chúng tôi có thể lưu và sử dụng cục bộ, đồng thời có thể được sử dụng bởi bất kỳ trình duyệt nào có cài đặt tiện ích mở rộng ứng dụng của chúng tôi một cách thích hợp.


Ví dụ: điều này mang lại cho tôi cùng một bộ dấu trang trên Chrome, Edge và Firefox mà không cần trình duyệt lưu dữ liệu dấu trang của tôi. Cả APP và nguồn API cho xbrowserSync đều nằm trong kho GitHub.


Yêu cầu thứ hai đối với trình tạo thư mục là mã chương trình của nó chạy dưới node.js trong Cửa sổ lệnh trên máy Windows 10 của tôi. Sau đây là chương trình nút đơn giản có tên handler.js .

 #!/usr/bin/env node // --------------------------------- // Simple bibliography file handler Node Server API // --------------------------------- const fs = require('fs') const path = require('path') //const dn = require('./dirname'); var filePath = './bibliotest.json'; //the biblio bookmark file var biblio = ''; var str = ''; var searchValue = 'url:'; // -------------- // read and parse biblio JSON file // ---------------- const jsonReader = () => { return new Promise(resolve => { str = fs.readFile(filePath, (err, fd) => { object = JSON.parse(fd); str = object.children; // strip outer layer biblio = str; // count biblio array elements count = biblio.length; resolve (str); //return str; }) }) } // async function performAsyncFunctions(){ // list of promises to execute sequentially const firstRequest = await jsonReader(); //console.log('\nAll tasks complete.'); console.log('==== function complete ====\n') console.log('count = ', count) var html = '<html><lang = en><head></head><body>'; for (let i = 0; i < count; i++) { html = html + '<h3><i>id'+[i]+':</i>&nbsp;<a href='+biblio[i].url+'>'+biblio[i].title+'</a></h3><p>'+biblio[i].description+'</p>\n'; } html = html + '</body></html>'; console.info(html); const content = html try { const data = fs.writeFileSync('./bibliotest.html', content) //file written successfully } catch (err) { console.error(err) }} // --------------- performAsyncFunctions(); // console.log('biblio = ', object) // ------------------


Tệp đánh dấu định dạng JSON

Từ menu tiện ích mở rộng trình duyệt xbrowserSync , chúng tôi có thể chọn lưu bản sao lưu dữ liệu dấu trang của mình. Bản sao lưu được lưu dưới dạng tệp JSON. Trong ví dụ của chúng tôi ở đây, tất cả dấu trang đều nằm trong thư mục dấu trang có tên “MuskArticle”.


Chúng tôi chỉnh sửa JSON dấu trang để chỉ trích xuất thư mục dấu trang mà chúng tôi muốn sử dụng trong thư mục của mình. Tệp JSON này được hiển thị ở đây bên dưới.

 { "title": "__MuskArticle", "children": [ { "title": "Musk's X caught throttling outbound links to websites he doesn't like", "url": "https://www.msn.com/en-us/news/technology/musk-sx-caught-throttling-outbound-links-to-websites-he-doesn-t-like/ar-AA1fmbYw?ocid=windirect&cvid=3ff1100b6fba4172b3b0b8b9c6bbc7e0&ei=185", "description": "Elon Musk claims that he bought Twitter, now called X, to preserve free speech. He claimed that everyone's voice should be heard. This is a great thing for free speech and must surely be a coincidence, right? Elon Musk's X was this week caught throttling outbound links to several sites, coincidentally ones that the billionaire has complained about or feuded with in the past.… Links directing users of the website formerly…", "id": 6643 }, { "title": "Mark Cuban takes another jab at Elon Musk's business practices", "url": "https://www.msn.com/en-us/money/companies/mark-cuban-takes-another-jab-at-elon-musk-s-business-practices/ar-AA1fmrNQ?ocid=windirect&cvid=528ef4be846842fa87342b96d92d0dfa&ei=35", "description": "These two billionaires are not best buds.", "id": 6664 }, { "title": "Twitter now makes you PAY to access one of its most popular features", "url": "https://www.msn.com/en-us/money/technology/twitter-now-makes-you-pay-to-access-one-of-its-most-popular-features/ar-AA1flk5Y?ocid=windirect&cvid=ee4ba03e4c414849a910c7f25dbfd759&ei=27", "description": "X (formerly Twitter) has started diverting users to a paid-subscription sign-up page when they try to access TweetDeck.", "id": 6665 }, { "title": "NYU Professor Locked Out of Twitter After Reportedly Declining to Meet With Elon Musk", "url": "https://www.msn.com/en-us/news/technology/nyu-professor-locked-out-of-twitter-after-reportedly-declining-to-meet-with-elon-musk/ar-AA1fmedB?ocid=windirect&cvid=a7973a74a0d24f2287ea421bbd70f5ea&ei=42", "description": "New York University professor and Kara Swisher's podcasting buddy Scott Galloway voiced his outrage at being banned from posting on Twitter in a Threads post on Tuesday. Galloway claims he's been locked out of Twitter (aka X) two days after allegedly declining an invitation to meet with the chief…", "id": 6666 }, { "title": "A federal judge wondered if Elon Musk was trying to 'cozy up' to Trump by trying to inform him about a search warrant into his social media account", "url": "https://www.msn.com/en-us/news/politics/a-federal-judge-wondered-if-elon-musk-was-trying-to-cozy-up-to-trump-by-trying-to-inform-him-about-a-search-warrant-into-his-social-media-account/ar-AA1flSZV?ocid=windirect&cvid=ee4ba03e4c414849a910c7f25dbfd759&ei=51", "description": "In January, federal prosecutors obtained a search warrant to obtain information from Trump's personal Twitter account.", "id": 6667 } ] }


Tệp thư mục

Nếu chúng tôi gửi tệp JSON ở trên tới chương trình xử lý node.js của mình, nó sẽ tạo ra danh sách thư mục định dạng HTML sau.


Phần kết luận

Vì vậy, chúng tôi đã thấy cách chúng tôi có thể tận dụng việc sử dụng xbrowserSync làm trình quản lý dấu trang để tạo thư mục từ dữ liệu dấu trang của mình. Cảm ơn bạn đã đọc và hy vọng thông tin này hữu ích với bạn. Như mọi khi, ý kiến, đề xuất và phê bình đều được hoan nghênh.