使用Python开发Chrome插件
admin
2023-07-30 21:59:42
0

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

创建一个谷歌Chrome插件

首先,我们必须创建一个清单文件:manifest.json。

12345678910111213141516 {  "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的文件:

123456789101112131415161718192021222324252627282930313233343536 &lt;!doctype html&gt;&lt;! This page is shown when the extension button is clicked, because the &quot;browser_action&quot; field in manifest.json contains the &quot;default_popup&quot; key with value &quot;popup.html&quot;. &gt;&lt;html&gt;  &lt;head&gt;    &lt;title&gt;Getting Started Extension&#039;s Popup</title>    &lt;style&gt;      body {        fontfamily: &quot;Segoe UI&quot;, &quot;Lucida Grande&quot;, Tahoma, sansserif;        fontsize: 100%;      }      #status {        /* avoid an excessively wide status text */        whitespace: pre;        textoverflow: ellipsis;        overflow: hidden;        maxwidth: 400px;      }    &lt;/style&gt;     &lt;!       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     &gt;    &lt;script src=&quot;popup.js&quot;&gt;&lt;/script&gt;  &lt;/head&gt;  &lt;body&gt;    &lt;div id=&quot;status&quot;&gt;&lt;/div&gt;    &lt;img id=&quot;imageresult&quot; hidden&gt;  &lt;/body&gt;&lt;/html&gt;

最后得到一个图标,并保存为icon.png。打开chrome://extensions,点击开发者模式。点击“加载未打包扩展程序”,选择文件夹,点击OK。

为Chrome扩展程序添加Python

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

12345678910111213141516171819202122 &lt;!DOCTYPE html PUBLIC &quot;//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;&lt;html&gt;&lt;head&gt;&lt;meta charset=&quot;iso88591&quot;&gt;&lt;style&gt;body {        margin: 0 !important;    padding: 0 !important;    width: 800;} #frame {    overflow: hidden;    width:790;    height:324;}&lt;/style&gt;&lt;/head&gt;&lt;body onLoad=&quot;&quot;&gt;&lt;iframe src=http://brython.info/console.html id=&quot;frame&quot; seamless=&quot;seamless&quot; scrolling=&quot;no&quot;&gt;&lt;/iframe&gt;&lt;/body&gt;&lt;/html&gt;

重启下你的插件,你就会在你的谷歌Chrome浏览器中得到一个Python(Brython)解释器。

运行你自己的脚本

为了能够运行你自己的脚本,简单地修改一下popup.html框架中的url即可:

1 &lt;iframe src=&quot;BRYTHON SCRIPT URL&quot; id=&quot;frame&quot; seamless=&quot;seamless&quot; scrolling=&quot;no&quot;&gt;&lt;/iframe&gt;

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

总结:

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

相关内容

热门资讯

500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
python绘图库Matplo... 本文简单介绍了Python绘图库Matplotlib的安装,简介如下: matplotlib是pyt...
Prometheus+Graf... 一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在Sound...