>該教程指導您創建一個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中文網其他相關文章!