使用Python开发chrome插件

本文由 伯乐在线 - xianhu 翻译,Daetalus 校稿。未经许可,禁止转载!
英文出处:pythonspot.com。欢迎加入翻译小组html

谷歌Chrome插件是使用HTML、JavaScript和CSS编写的。若是你以前历来没有写过Chrome插件,我建议你读一下这个。在这篇教程中,咱们将教你如何使用Python代替JavaScript。python

建立一个谷歌Chrome插件ajax

首先,咱们必须建立一个清单文件:manifest.json。chrome

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
   "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的文件:json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!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。api

为Chrome扩展程序添加Python浏览器

如今你拥有了最基本的权利,咱们能够在代码中添加Python。为了能在一个浏览器中运行Python,你有不少个选择,包括Brython和emcascripten。咱们决定使用Brython。咱们将从一个服务器运行Brython脚本。改变popup.html的内容:服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!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)解释器。框架

运行你本身的脚本less

为了可以运行你本身的脚本,简单地修改一下popup.html框架中的url便可:

1
< iframe src = "BRYTHON SCRIPT URL" id = "frame" seamless = "seamless" scrolling = "no" ></ iframe >

这个脚本应该运行在你本身的服务器上。你能够从网上运行任意的Brython脚本。利用Brython,你能够简单地在脚本标签中输入Python代码。看一下这个Brython的例子,或者简单地浏览下这个网站

总结:

Chrome插件是使用HTML、JavaScript和CSS建立的。咱们想知道在谷歌Chrome插件中可否使用Python代码。咱们最终获得了一个浏览器中的Python解释器和执行Python脚本的能力。记住,这只是个实现性的结果,只是一个玩具,在这一点上,我不建议你将全部的插件都移植或创建在Brython上。

关于做者: xianhu

相关文章
相关标签/搜索