修改Bookmarklet以识别粗体、斜体文本及链接并导出至WorkFlowy
需求与解决方案
问题说明
用户完全不懂编程,希望修改Rawbitz开发的Bookmarklet,使其能够识别网页中的粗体、斜体文本及链接,并正确导出至WorkFlowy,此前尝试通过ChatGPT解决未成功。
原代码如下:
javascript:(function C2W_2_9(){ function eOPML(str){ return str.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/"/g,'"').replace(/( )/g,' ') } function copyThat(str){ const t=document.createElement("textarea"); t.value=str; document.body.appendChild(t); t.select(); const success=document.execCommand("copy"); document.body.removeChild(t); return success} function toastMsg(str,sec,err){ WF.showMessage(str,err); setTimeout(WF.hideMessage,(sec||2)*1e3) } const IS_WF=typeof WF==="object"&&typeof WF.currentItem==="function"; const copyAndToastExt=(str,message,isWF)=>copyThat(str)?isWF?toastMsg(message,2):null:prompt(" Copy failed. Copy this manually.",str); const name=eOPML(document.title.replace(/ - WorkFlowy$/,"")); const bullet=IS_WF?`<i>See: "${name}"</i>`:name; const selection=window.getSelection().toString(); const link=eOPML(location.href); const note=selection?`..${link} ${eOPML(selection)}`:link; const clip=`<opml><body><outline text="${bullet}" _note="${note}" /></body></opml>`; copyAndToastExt(clip,"Clip copied!",IS_WF) })();
修改后的Bookmarklet代码
javascript:(function C2W_Enhanced(){ // OPML文本转义处理 function eOPML(str){ return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/\n/g,' '); } // 复制内容到剪贴板 function copyThat(str){ const t=document.createElement("textarea"); t.value=str; document.body.appendChild(t); t.select(); const success=document.execCommand("copy"); document.body.removeChild(t); return success; } // WorkFlowy内置消息提示 function toastMsg(str,sec,err){ WF.showMessage(str,err); setTimeout(WF.hideMessage,(sec||2)*1000); } // 检测当前是否在WorkFlowy页面 const IS_WF=typeof WF==="object"&&typeof WF.currentItem==="function"; // 复制操作统一处理函数 const copyAndToastExt=(str,message,isWF)=>copyThat(str)?isWF?toastMsg(message,2):null:prompt("复制失败,请手动复制以下内容:",str); // 将选中的富文本转换为WorkFlowy支持的格式 function convertSelectionToWFFormat(){ const selection=window.getSelection(); if(!selection.rangeCount) return ""; const range=selection.getRangeAt(0); const container=document.createElement("div"); container.appendChild(range.cloneContents()); // 转换网页链接为WorkFlowy格式:[显示文本](链接地址) container.querySelectorAll("a").forEach(link=>{ const url=link.getAttribute("href"); const text=link.textContent; const wfLink=`[${text}](${url})`; link.parentNode.replaceChild(document.createTextNode(wfLink),link); }); // 转换粗体文本为WorkFlowy格式:**加粗内容** container.querySelectorAll("b, strong").forEach(bold=>{ const text=bold.textContent; const wfBold=`**${text}**`; bold.parentNode.replaceChild(document.createTextNode(wfBold),bold); }); // 转换斜体文本为WorkFlowy格式:*斜体内容* container.querySelectorAll("i, em").forEach(italic=>{ const text=italic.textContent; const wfItalic=`*${text}*`; italic.parentNode.replaceChild(document.createTextNode(wfItalic),italic); }); // 清理多余空格与换行,返回纯文本格式 return container.textContent.replace(/\s+/g,' ').trim(); } // 生成最终导出的OPML内容 const pageTitle=eOPML(document.title.replace(/ - WorkFlowy$/,"")); const bulletText=IS_WF?`<i>See: "${pageTitle}"</i>`:pageTitle; const formattedContent=convertSelectionToWFFormat(); const pageLink=eOPML(location.href); const noteContent=formattedContent?`..${pageLink} ${eOPML(formattedContent)}`:pageLink; const opmlClip=`<opml><body><outline text="${bulletText}" _note="${noteContent}" /></body></opml>`; copyAndToastExt(opmlClip,"已复制到剪贴板!",IS_WF); })();
核心改动说明
- 新增富文本转换逻辑:专门编写
convertSelectionToWFFormat函数,提取选中区域的HTML结构,将网页中的链接、粗体、斜体分别转换为WorkFlowy原生支持的格式。 - 修正转义函数:原
eOPML的转义规则存在冗余,调整后确保OPML格式的文本转义正确,避免导入WorkFlowy时出现乱码。 - 优化内容整洁度:自动清理转换后的多余空格和无效HTML标签,让导出的内容更符合阅读习惯。
- 中文提示优化:将复制失败的提示改为中文,降低使用门槛。
内容的提问来源于stack exchange,提问作者Luigi
相关产品推荐
相关产品推荐

