chrome 前端插件_如何将前端项目转换为chrome扩展

news/2024/7/10 1:05:34 标签: vue, chrome, java, html, js
htmledit_views">

html" title=chrome>chrome 前端插件

The Chrome extension can greatly extend the functions of Chrome and has a huge market and commercial value. As a front-end programmer, can you write a Chrome extension?

Chrome扩展程序可以极大地扩展Chrome的功能,并具有巨大的市场和商业价值。 作为前端程序员,您可以编写Chrome扩展程序吗?

Chrome extensions are written using HTML, CSS, and JavaScript, and we don’t need to learn a new development language. Every front-end developer can easily transform their existing project into a Chrome extension project.

Chrome扩展程序是使用HTML,CSS和JavaScript编写的,我们无需学习新的开发语言。 每个前端开发人员都可以轻松地将其现有项目转换为Chrome扩展项目。

Suppose we now have a very simple front-end project in which there is currently only one index.html file. This file is used to display the current system time:

假设我们现在有一个非常简单的前端项目,其中目前只有一个index.html文件。 此文件用于显示当前系统时间:

Image for post

Okay, so let’s get started with transforming this very simple front-end project into a Chrome extension.

好的,让我们开始将这个非常简单的前端项目转换为Chrome扩展程序。

Image for post

manifest.json (manifest.json)

Chrome specifies that all extension projects must have a manifest.json file in the root directory, which identifies an extension project and performs various configurations.

Chrome浏览器指定所有扩展项目必须在根目录中具有manifest.json文件,该文件标识扩展项目并执行各种配置。

Now let’s add a manifest file to the root directory of the project and write the following.

现在,将清单文件添加到项目的根目录,并编写以下内容。

{
"name": "My Extension",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_popup": "index.html"
}
}
Image for post

The name field declares the name of our extension, which will be the name of your extension if you upload it to the Chrome Web Store in the future. version is the version number of our project. The manifest_version is what the version number of our manifest file is, and so far this value is always 2.

name字段声明了我们的扩展程序的名称,如果将来您将其上载到Chrome网上应用店,它将作为扩展程序的名称。 version是我们项目的版本号。 manifest_version是清单文件的版本号,到目前为止,该值始终为2。

The browser_action field is used to configure our extension’s behaviors. When we click on the extension in Chrome, we open the rendered page from the HTML file corresponding to default_popup.

browser_action字段用于配置扩展程序的行为。 当我们单击Chrome中的扩展程序时,我们从与default_popup对应HTML文件中打开呈现的页面。

The GIF below is what the extension looks like for now.

下面的GIF是扩展名的样子。

Image for post

安装扩展 (Install Extension)

With the configuration complete, we are now ready to install the extension we just wrote into Chrome browser so that we can test the project.

完成配置后,我们现在就可以安装刚刚写入Chrome浏览器的扩展程序,以便我们测试项目。

There are three steps to installing a extension:

安装扩展程序分为三个步骤:

  • open html" title=chrome>chrome://extensions/

    打开html" title=chrome>chrome:// extensions /
  • turn developer mode on

    开启developer mode

  • unpload unpack project

    解压解压项目
Image for post
Image for post

HTML脚本 (HTML Script)

Careful readers may have noticed that JavaScript scripts in HTML are not executed.

细心的读者可能已经注意到,HTML中JavaScript脚本未执行。

We have this script in our HTML file:

我们HTML文件中包含以下脚本:

<script type="text/html" title=java>javascript">
let interval = setInterval(() => {
document.getElementById("localTime")
.innerText = new Date().toString()
}, 1000);
</script>

But this code is not executed. This is because Chrome dictates that JavaScript scripts from HTML files in extensions can only be placed in separate files, not embedded in HTML files. So we need to create an extra .js file and put the JavaScript code there.

但是此代码未执行。 这是因为Chrome要求扩展程序中HTML文件中JavaScript脚本只能放置在单独的文件中,而不能嵌入HTML文件中。 因此,我们需要创建一个额外的.js文件,并将JavaScript代码放在此处。

Image for post

And in the HTML file, we cam simply reference this code.

在HTML文件中,我们仅引用此代码即可。

Image for post

At this point, we’re reloading our extension, and we can see that the JavaScript script is working properly.

至此,我们正在重新加载扩展,并且可以看到JavaScript脚本正常运行。

Image for post
Click this to reload extension
点击此按钮以重新加载扩展程序
Image for post
The script works.
该脚本有效。

图标 (icon)

Next, let’s add a nice looking icon to the extension.

接下来,让我们为扩展程序添加一个漂亮的图标。

First we prepare an image and then put it into the project.

首先,我们准备一张图像,然后将其放入项目中。

Image for post
Image for post

Next we need to configure the manifest file to declare our logo. So to be specific, we could write it this way:

接下来,我们需要配置清单文件以声明我们的徽标。 因此,具体来说,我们可以这样写:

Image for post

The default_icon field is used to declare icons. Now refresh our extension and you will see that the logo has changed.

default_icon字段用于声明图标。 现在刷新我们的扩展名,您将看到徽标已更改。

Image for post

后台脚本 (The background script)

One of the drawbacks of previous JavaScript scripts is that they don’t start running until the user clicks on the extension icon. But a lot of times, we want the script to start executing as soon as the user starts the browser, so what do we do?

以前JavaScript脚本的缺点之一是它们只有在用户单击扩展图标后才开始运行。 但是很多时候,我们希望脚本在用户启动浏览器后立即开始执行,那么我们该怎么办?

First, let’s simply write a new script:

首先,让我们简单地编写一个新脚本:

alert("Hello! -- from background/main.js")setTimeout(()=>{
alert("Hi! -- from background/main.js")
}, 2000)
Image for post

Then configure the manifest file.

然后配置清单文件。

Image for post

The background/script field is used to configure the script to run in the background. It is an array that can be configured with multiple files running in the background.

background/script字段用于将脚本配置为在后台运行。 它是一个可以配置为在后台运行多个文件的阵列。

Next, let’s reload the extension and test how it works.

接下来,让我们重新加载扩展并测试其工作方式。

Image for post
it works.
有用。

内容脚本 (Content Script)

Sometimes we want extensions that execute scripts after the user has opened a web page. For example, when the user opens Medium.com, “Hello Medium” will pop up. When a user opens GitHub.com, “Hi, GitHub” pops up.

有时,我们希望扩展程序在用户打开网页后执行脚本。 例如,当用户打开Medium.com时,将弹出“Hello Medium” 。 当用户打开GitHub.com时,会弹出“Hi, GitHub”

To do that, we need to use content script. Conten script is a script that is executed only when the user opens certain web pages.

为此,我们需要使用内容脚本。 内容脚本是仅当用户打开某些网页时才执行的脚本。

Let’s start by writing two simple script files.

让我们从编写两个简单的脚本文件开始。

Image for post
Image for post

Then configure the manifest file.

然后配置清单文件。

Image for post
"content_scripts": [
{
"matches": ["*://medium.com/*"],
"js": ["content/medium.com.js"]
}, {
"matches": ["*://github.com/*"],
"js": ["content/github.com.js"]
}
]

"*://medium.com/*” will match any page on the Medium, so when we open any Medium page, “content/medium.com.js will be executed.

"*://medium.com/*”将与"*://medium.com/*”上的任何页面匹配,因此当我们打开任何“媒介”页面时,将执行“content/medium.com.js

"*://github.com/*” will match any page on the GitHub, so when we open any GitHub page, “content/github.com.js will be executed.

"*://github.com/*”将匹配GitHub上的任何页面,因此当我们打开任何GitHub页面时,将执行“content/github.com.js

Image for post
Image for post

结论 (Conclusion)

Well, this article covers the basics of developing Chrome extensions, and now you can turn one of your own front-end projects into a Chrome extension.

好的,本文介绍了开发Chrome扩展程序的基础知识,现在您可以将自己的前端项目之一转换为Chrome扩展程序。

The source code for the project is available on GitHub:

该项目的源代码可在GitHub上找到:

翻译自: https://medium.com/html" title=java>javascript-in-plain-english/how-to-transform-a-frontend-project-into-a-html" title=chrome>chrome-extension-675d0821cc6d

html" title=chrome>chrome 前端插件


http://www.niftyadmin.cn/n/1468274.html

相关文章

树莓派配置php环境变量,[操作系统]树莓派配置apache php

[操作系统]树莓派配置apache php0 2016-07-09 13:00:07Apache MySql Php&#xff0e;1、安装ApacheApache可以用下面的命令来安装 sudo apt-get install apache2Apache默认路径是/var/www/其配置文件路径为&#xff1a; /etc/apache2/可以通过&#xff1a;sudo nano /etc/apa…

python shell打不开_Python Shell没有运行

我在跑步Python.org网站版本2.7 64位Windows Vista 64位使用Scrapy。当我通过命令Shell运行它时&#xff0c;我有一些代码正在工作(除了命令Shell无法识别非Unicode字符的一些问题之外)&#xff0c;但是当我尝试通过Python空闲运行脚本时&#xff0c;我得到以下错误消息&#x…

如何在托梁中处理自己

Not long ago I introduced a new framework called Joist. As pointed out in that introduction, one of the more unique features of Joist is the concept of “handlers” and because of this I thought it would be worth digging into what handlers are and what you…

matlab ncut谱聚类,NCUT 归一化分割、谱聚类之代码调试问题

1 相比于c&#xff0c;matlab的效率较低&#xff0c;为了解决这个问题&#xff0c;大家在matlab中调用c&#xff0c;也就是说matlab调用的一些函数&#xff0c;本身是由c编写完成的&#xff0c;执行的时候也是在c编译器中执行。实现这个功能的媒介是后缀为mex的文件&#xff0c…

使用babel转换js为_如何仅使用babel即可快速转换javascript babel js简介

使用babel转换js为JavaScript&#xff1a;工具 (JavaScript: Tooling) You might have heard of Webpack, Rollup, or perhaps Parcel. These are some of the widely popular (web) bundling tools. What is bundling you ask? I think you are a little late to the party. …

join left semi_Hive 中的 LEFT SEMI JOIN 与 JOIN ON

hive 的 join 类型有好几种&#xff0c;其实都是把 MR 中的几种方式都封装实现了&#xff0c;其中 join on、left semi join 算是里边具有代表性&#xff0c;且使用频率较高的 join 方式。1、联系他们都是 hive join 方式的一种&#xff0c;join on 属于 common join(shuffle j…

php数据库如何创建表单,ThinkPhp学习笔记——创建数据数据库中的表单

http://localhost/phpMyAdmin/用户名&#xff1a;root密码&#xff1a;root//创建数据库↓进入页面主页后点击“数据库”新建数据库&#xff1a;输入数据库名称为blog选择数据库编码格式&#xff1a;utf8_general_ci点击创建//创建数据库↑点击blog表单tp_admin(后台的管理账号…

php服务器响应超时,服务端的网络连接发生异常,服务器繁忙导致响应超时!

版本号:7.0.5业务场景(如下):总账辅助等多个模块下&#xff0c;点击修改等操作时&#xff0c;提示与服务端的网络连接发生异常&#xff0c;服务器繁忙导致响应超时&#xff01;预期效果(如下):异常信息(异常类型&#xff1a;Genersoft.Platform.Core.Error.GSPException)异常标…