>该教程指导您创建一个Chrome扩展名,该扩展为Google文档添加了持久的单词计数。 它通过在状态栏中提供不断更新的单词计数来增强Google Docs用户体验。
密钥功能:
Tools > Extensions
>
的文件,其中包含以下内容:
manifest.json
>
使用此代码
{ "name": "GDWC", "version": "0.1", "description": "Word count statusbar for Google Docs!", "background" : { "scripts": ["background.js"] }, "page_action" : { "default_icon" : "icon.png", "default_title" : "GDWC statusbar is active" }, "content_scripts": [ { "matches": ["https://docs.google.com/document/*"], "js": ["jq.js", "main.js"], "run_at": "document_idle" } ], "icons": { "48": "icon48.png", "128": "icon128.png" } }
此脚本在地址栏中显示了扩展名的图标。
>background.js
步骤3:状态栏html(statusbar.html)
chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { chrome.pageAction.show(sender.tab.id); sendResponse({}); } );
创建
:这会创建视觉状态栏。 切记包括原始输入中的CSS。
>statusbar.html
步骤4:主javascript(main.js)
<div id="GDWC_statusBar"> <a href="https://www.php.cn/link/1c09cec8e3fb5f6dd4fd22a5c644d3e5">GDWC</a> <span class="GDWC_statusBarSeparator"></span> <span id="GDWC_wordsTotal">Warming up...</span> </div> <style> /* CSS styles for the status bar */ /* ... (same CSS as in original input) ... */ </style>
创建
:此脚本注入状态栏HTML并实现单词计数逻辑。 您需要在您的项目中包括
(jQuery的缩小版本)。>
main.js
>
$.get(chrome.extension.getURL("statusbar.html"), {}, function(data) {$('body').append(data);}, 'html'); chrome.extension.sendRequest({}, function(response) {}); $(document).ready(function(){ countWords(); }); function countWords() { var number = 0; $('span.kix-lineview-text-block').each(function(i, obj){ number += $(obj).text().split(/s+/).length; }); $('#GDWC_wordsTotal').text(number + ' total words'); timeout = setTimeout('countWords()', 5000); }
jq.js
,,)。
>icon.png
,启用开发人员模式,然后单击“加载打开包装”。icon48.png
icon128.png
选择“ gdwc”文件夹。以上是构建您自己的Chrome扩展名:Google Docs Word Count工具的详细内容。更多信息请关注PHP中文网其他相关文章!