参考书目生成器操作的第一个要求是其内容数据源,该源由名为xbrowserSync的浏览器书签管理器应用程序创建的 JSON 对象组成。该应用程序是一个与浏览器无关的书签管理器,分为两部分。
一部分是浏览器扩展,另一部分是服务器 API,它将书签数据作为MongoDB数据文件进行管理。关于服务器 API,我们需要了解的只是它的 URL。该浏览器扩展程序可以在在线应用程序“商店”中找到,例如 Chrome 浏览器的Google Play 。
基本思想是xbrowserSync应用程序将您的书签数据存储在存储在 API 服务器上的加密数据库中。数据库本身实际上是一个 JSON 对象文件,我们可以在本地保存和使用它,并且可以由任何适当安装了我们的应用程序扩展的浏览器使用。
例如,这可以让我在 Chrome、Edge 和 Firefox 上获得相同的书签集,而无需让浏览器保存我的书签数据。 xbrowserSync的 APP 和 API 源均位于GitHub 存储库中。
参考书目生成器的第二个要求是其程序代码在我的Windows 10计算机上的命令窗口中的node.js下运行。下面是这个名为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> <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) // ------------------
从xbrowserSync浏览器扩展菜单中,我们可以选择保存书签数据的备份。备份保存为 JSON 文件。对于我们这里的示例,所有书签都位于名为“MuskArticle”的书签文件夹中。
我们编辑书签 JSON 以仅提取我们想要在参考书目中使用的书签文件夹。该 JSON 文件如下所示。
{ "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 } ] }
如果我们将上面的 JSON 文件提交到Node.js处理程序,它将生成以下 HTML 格式的参考书目列表。
因此,我们已经了解了如何利用 xbrowserSync 作为书签管理器从书签数据生成参考书目。感谢您的阅读,希望这些信息对您有用。一如既往,欢迎提出意见、建议和批评。