基于Python开发chrome插件的方法分析

作者:juggd 时间:2023-04-22 14:41:33 

本文实例讲述了基于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)解释器。

基于Python开发chrome插件的方法分析

运行你自己的脚本

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


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

这个脚本应该运行在你自己的服务器上。你可以从网上运行任意的Brython脚本。利用Brython,你可以简单地在脚本标签中输入Python代码。

总结:

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

希望本文所述对大家Python程序设计有所帮助。

来源:http://www.cnblogs.com/rrxc/p/4496380.html

标签:Python,chrome插件
0
投稿

猜你喜欢

  • Pytorch损失函数torch.nn.NLLLoss()的使用

    2021-02-07 16:08:57
  • 使用pip下载时提示"You are using pip version 8.1.1, however version 22.1 is available."错误解决

    2023-08-02 18:04:49
  • Python SQLite3数据库操作类分享

    2023-08-24 01:04:02
  • PHP 中关于ord($str)&gt;0x80的详细说明

    2024-05-13 09:24:13
  • 如何在页面中对不同的数据进行相同的处理?

    2010-06-26 12:30:00
  • ORACLE11g随RHEL5系统自动启动与关闭的设置方法

    2009-08-31 12:43:00
  • Python3内置模块pprint让打印比print更美观详解

    2022-02-04 01:55:28
  • 对SQL Server聚集索引的指示综合描述

    2010-08-31 14:25:00
  • js 中以 ... 为前缀的几种用法详解

    2024-04-18 09:40:38
  • js鼠标事件大全

    2007-08-13 15:56:00
  • vue如何使用formData传递文件类型的数据

    2024-04-30 10:33:24
  • 被jQuery折腾得半死,揭秘为何jQuery为何在IE/Firefox下均无法使用

    2024-05-11 09:33:27
  • OpenCV实现人脸识别

    2023-01-07 02:38:58
  • pandas分批读取大数据集教程

    2023-01-13 16:45:32
  • PHP结合vue导出excel出现乱码的解决方法分享

    2023-05-30 09:18:25
  • python实用的快捷语法技巧大全

    2022-12-11 11:33:35
  • Python使用matplotlib绘制多个图形单独显示的方法示例

    2022-02-13 09:45:44
  • Python线程之定位与销毁的实现

    2023-09-25 11:47:19
  • SQL Server 2005通用分页存储过程及多表联接应用

    2024-01-13 22:39:31
  • MySQL的一级防范检查列表

    2011-12-14 18:39:22
  • asp之家 网络编程 m.aspxhome.com