本文實例講述了基于Python開發chrome插件的方法。分享給大家供大家參考,具體如下:
谷歌Chrome插件是使用HTML、JavaScript和CSS編寫的。如果你之前從來沒有寫過Chrome插件,我建議你讀一下這個。在這篇教程中,我們將教你如何使用Python代替JavaScript。
創建一個谷歌Chrome插件
首先,我們必須創建一個清單文件:manifest.json。
{ "manifest_version": 2, "name": "Python Chrome Plugin", "description": "This extension runs Python code.", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "activeTab", "https://ajax.googleapis.com/" ]}
然后創建一個名為popup.html的文件:
<!doctype html><!-- This page is shown when the extension button is clicked, because the "browser_action" field in manifest.json contains the "default_popup" key with value "popup.html". --><html> <head> <title>Getting Started Extension's Popup</title> <style> body { font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif; font-size: 100%; } #status { /* avoid an excessively wide status text */ white-space: pre; text-overflow: ellipsis; overflow: hidden; max-width: 400px; } </style> <!-- - JavaScript and HTML must be in separate files: see our Content Security - Policy documentation[1] for details and explanation. - - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy --> <script src="popup.js"></script> </head> <body> <div id="status"></div> <img id="image-result" hidden> </body></html>
最后得到一個圖標,并保存為icon.png。打開chrome://extensions,點擊開發者模式。點擊“加載未打包擴展程序”,選擇文件夾,點擊OK。
為Chrome擴展程序添加Python
現在你擁有了最基本的權利,我們可以在代碼中添加Python。為了能在一個瀏覽器中運行Python,你有很多個選擇,包括Brython和emcascripten。我們決定使用Brython。我們將從一個服務器運行Brython腳本。改變popup.html的內容:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta charset="iso-8859-1"><style>body { margin: 0 !important; padding: 0 !important; width: 800;}#frame { overflow: hidden; width:790; height:324;}</style></head><body onLoad=""><iframe src=http://brython.info/console.html id="frame" seamless="seamless" scrolling="no"></iframe></body></html>
重啟下你的插件,你就會在你的谷歌Chrome瀏覽器中得到一個Python(Brython)解釋器。
新聞熱點
疑難解答