chrome扩展程序

We all like surfing the web. And we all like things to be at the touch of our fingertips. Why not create something that caters to these two absolute truths?

我们都喜欢上网。 而且我们都希望事情触手可及。 为什么不创造出满足这两个绝对真理的东西呢?

In this article, I’ll explain the building blocks of a Chrome extension. Afterwards, you’ll just have to think of a good idea as an excuse to make one.

在本文中,我将介绍Chrome扩展程序的基本组成部分。 之后,您只需要将一个好主意视为创造一个主意的借口。

为什么选择Chrome? (Why Chrome?)

Chrome is by far the most popular web browser. Some estimations are as high as 59%. So, if you want to reach as many people as you can, developing a Chrome extension is the best way to do it.

Chrome是迄今为止最受欢迎的网络浏览器。 一些估计高达59% 。 因此,如果您想吸引尽可能多的人,开发Chrome扩展程序就是最好的方法。

⚠️ To be able to publish a Chrome extension, you need to have a developer account which entails a $5 one-time signup fee.

⚠️要发布Chrome扩展程序,您需要拥有一个开发人员帐户,该帐户需要支付5美元的一次性注册费 。

Each Chrome extension should have these components: the manifest file, popup.html and popup.js and a background script. Lets take a look at them.

每个Chrome扩展程序应具有以下组件: 清单文件popup.htmlpopup.js以及后台脚本。 让我们看看它们。

Chrome扩展程序由什么组成? (What makes up a Chrome extension?)

清单文件 (The Manifest File)

What is a manifest file? It is a text file in JSON (JavaScript Object Notation) format that contains certain details about the extension you will be developing.

什么是清单文件? 这是JSON( JavaScript对象表示法 )格式的文本文件,其中包含有关您将开发的扩展的某些详细信息。

Google uses this file to acquire details about your extension when you will publish it. There are required, recommended and optional fields.

发布时,Google会使用此文件获取有关您的扩展程序的详细信息。 有必填字段, 推荐字段和可选字段。

需要 (Required)

{"manifest_version": 2,"name": "My Chrome Extension","version": "1.0","default_locale": "en"
}
  • manifest_version - Version of the manifest file format. As of Chrome 18, version 1 is deprecated

    manifest_version清单文件格式的版本。 从Chrome 18开始,版本1已弃用

  • name - Can be up to 45 characters. Used to display your extension’s name in the following places: Install dialog, Extension management UI, Chrome Web Store

    name -最多45个字符。 用于在以下位置显示扩展程序的名称:“安装”对话框,扩展程序管理UI,Chrome网上应用店

  • version - Version of your Chrome Extension. Can be up to four digits separated by dots (e.g., 1.0.0.0)

    version -您的Chrome扩展程序的版本。 最多可以由点分隔的四位数(例如1.0.0.0)

  • default_locale - This folder resides inside the _locals folder and it contains the default string literals. The _locals folder is used for internationalization (allowing your extension to support multiple languages). It is a required field if a _locals folder exists, otherwise, it should not be present

    default_locale此文件夹位于_locals文件夹中,并且包含默认字符串文字。 _locals文件夹用于国际化(允许您的扩展名支持多种语言)。 如果存在_locals文件夹, _locals必填字段,否则不应存在

If you want to support multiple languages, read more here.

如果您想支持多种语言,请在此处内容。

推荐的 (Recommended)

"description": "A plain text description","author": "Your Name Here","short_name": "shortName","icons": {"128":"icon128.png","48":"icon48.png","16":"icon16.png"},
  • description - You can use up to 132 characters to describe the extension

    description您最多可以使用132个字符来描述扩展名

  • short_name - Limited to 12 characters, it is used in cases where there is not enough space to display the full name of the extension (App Launcher and New Tab Page)

    short_name为12个字符,用于没有足够空间显示扩展名全名的情况(应用启动器和“新标签页”)

  • icons - The icons that represent the extension. Always include a 128X128 icon because it is used by the Chrome Web Store and during the installation of your extension

    icons代表扩展名的图标。 始终包含一个128X128图标,因为它由Chrome网上应用店和在安装扩展程序时使用

Optional fields are case specific, so we won’t go into them in this article.

可选字段是特定于大小写的,因此在本文中我们将不再赘述。

After covering the data needed for the manifest file, we can now move on to where we will actually be writing code for our extension, Popup and Background.

在覆盖了清单文件所需的数据之后,我们现在可以继续实际为扩展名Popup和Background编写代码。

弹出窗口和背景 (Popup and Background)

The popup refers to the main page users see when using your extension. It consists of two files: Popup.html and a JavaScript file, Popup.js.

弹出窗口是指用户在使用扩展程序时看到的主页。 它由两个文件组成: Popup.html和一个JavaScript文件Popup.js

Popup.html is the layout file for how your extension will look like. Depending on what your extension will do, the markup of this file will change.

Popup.html是扩展程序外观的布局文件。 根据扩展名的作用,此文件的标记将更改。

A background script is the only one that can interact with events that occur and use the Chrome API. To use background scripts you need to add the following to your manifest.json file:

后台脚本是唯一可以与发生的事件进行交互并使用Chrome API的脚本。 要使用后台脚本,您需要将以下内容添加到manifest.json文件中:

{"manifest_version": 2,"name": "My Chrome Extension","version": "1.0","background":{"scripts": ["yourScript.js"],"persistent": false}
}

The key scripts has a value of an array of script names.

关键scripts具有脚本名称数组的值。

persistent is a key with a boolean value which denotes the use of your extension with chrome.webRequest API to block or modify network requests. The Chrome.webRequest API does not work with non-persistent background pages.

persistent是具有布尔值的键,该值表示您的扩展程序与chrome.webRequest API配合使用以阻止或修改网络请求。 Chrome.webRequest API不适用于非永久背景页面。

您的扩展程序将如何打开 (How Your Extension Will Open)

Every Chrome extension adds a little icon to the toolbar at the top of your browser. Once the user clicks that icon, you can choose how you want your extension to open in the browser:

每个Chrome扩展程序都会在浏览器顶部的工具栏中添加一个小图标。 用户单击该图标后,您可以选择扩展程序在浏览器中的打开方式:

  1. It can override a new tab, so as not to disrupt the current user’s activity它可以覆盖一个新的选项卡,以免破坏当前用户的活动

2. You can open a small window in the user’s current tab, so as to keep the user in the same tab

2.您可以在用户的​​当前选项卡中打开一个小窗口,以使用户保持在同一选项卡中

Each choice has it’s consequences and it is up to you to decide what is the best option for you.

每个选择都有其后果,由您决定什么是最适合您的选择。

Below is the code needed to pull off each of the options. They will both use the same popup.html file outlined below:

下面是提取每个选项所需的代码。 他们都将使用以下概述的相同popup.html文件:

<html><head><title>Chrome Extension Example</title></head><body><h1>Hello From Extension</h1></body></html>

放在一起 (Putting It All Together)

覆盖新标签 (Override New Tab)

//In manifest.json
{"name": "ChromeExampleNewTab","version": "1.0","manifest_version": 2,"chrome_url_overrides": {"newtab": "popup.html"},"browser_action": {}, "permissions":[        "tabs"],"background":{        "scripts": ["background.js"],"persistent": false}
}//In background.js
chrome.browserAction.onClicked.addListener(function(tab) {chrome.tabs.create({'url': chrome.extension.getURL('popup.html')}, function(tab) {// Tab opened.});
});

在当前标签页中打开 (Open In The Current Tab)

//In manifest.js
{"name": "ChromeExample","version": "1.0","manifest_version": 2,"browser_action": {         "default_popup": "popup.html"}
}

Notice how we have overridden the browser_action key in both examples.

请注意,在两个示例中我们都是如何覆盖browser_action键的。

We have to do this, since we don’t want the browser to open a new tab in the regular fashion.

我们必须这样做,因为我们不希望浏览器以常规方式打开新标签页。

Also, since if we want to open a new tab with our extension, we must add a permissions key to the manifest and specify the tabs value. This lets the browser know we need the user’s permission to overwrite the default behavior of opening a new tab.

另外,由于如果要使用扩展名打开新标签页,则必须向清单添加权限密钥并指定标签页值。 这使浏览器知道我们需要用户的权限才能覆盖打开新选项卡的默认行为。

There is a whole lot more to Chrome extensions (messaging, context menus and storage to name a few). I have hopefully given you some insight into Chrome extensions. Maybe just enough to intrigue you to make one of your own!

Chrome扩展还有很多其他功能(消息传递,上下文菜单和存储等等)。 希望我能给您一些有关Chrome扩展程序的见解。 也许足以吸引您成为自己的一员!

And while you are at it, check one I have made here.

而当您在查看时,请检查一下我在这里制作的。

翻译自: https://www.freecodecamp.org/news/how-to-implement-a-chrome-extension-3802d63b5376/

chrome扩展程序

chrome扩展程序_如何实施Chrome扩展程序相关推荐

  1. chrome扩展程序_如何创建Chrome扩展程序

    chrome扩展程序 by Erika Tan 谭咏麟 如何创建Chrome扩展程序 (How to create a Chrome Extension) In this article, I wil ...

  2. chrome添加主题_如何在Chrome中添加和删除主题

    chrome添加主题 Google Chrome lets you personalize your experience with themes to make things a little mo ...

  3. chrome python插件_爬虫的Chrome插件推荐

    [1]Toggle JavaScript [2]JSON-handle 这个插件可以帮我们格式化Json串,从而让我们以一个更友好的方式查看Json内的信息. chrome商店下载地址:https:/ ...

  4. 渐进式web应用程序_为什么渐进式Web应用程序很棒,以及如何构建一个

    渐进式web应用程序 by Ankita Masand 通过Ankita Masand 为什么渐进式Web应用程序很棒,以及如何构建一个 (Why Progressive Web Apps are g ...

  5. linux 易语言窗口程序_易语言开发Linux程序

    令人兴奋的是易语言可以开发Linux程序,易语言是一个跨平台的开发工具,支持Windows及Linux.使用易语言开发Linux程序,您可以在Windows环境下编写基本于Windows的程序,及编写 ...

  6. 一个公众号可以绑定几个小程序_如何实现微信小程序和公众号的绑定

    再此之前,为什么要有小程序和公众号绑定这一个操作. 第一:小程序绑定公众号之后,公众号可以推文. 如下所示,公众号推文,文章内容由后台发布,因为公众号分为服务号和订阅号,订阅号每天可以发送一次,一次最 ...

  7. 万能门店小程序_超市门店微信小程序注册流程

    现在超市门店商家利用小程序卖货已经非常普遍,小程序可以便捷地连接线上线下通道,让商家不再局限于门店周围的流量,能够从微信获取更多客户,从而提升销量和店铺知名度.超市要想有自己的线上电商小程序,就得先注 ...

  8. 不同主体公众号跳转小程序_公众号和小程序的区别

    其实公众号和小程序都是微信的衍生功能,同时微信的产品.但是它们的功能不同.公众号更倾向于引流,主要是通过持续的输出有价值的文案视频图片来吸引大量的粉丝(顾客),并且培育粉丝最终获得收益.(在这里特别提 ...

  9. 渐进式web应用程序_通过渐进式Web应用程序吸引用户并增强他们的体验

    渐进式web应用程序 by Dave Gray 戴夫·格雷(Dave Gray) 通过渐进式Web应用程序吸引用户并增强他们的体验 (Engage your users and enhance the ...

  10. 专属海报小程序_剑3泡泡 | 小程序给你一份专属的账号海报!

    01按照惯例,这里是简介 paopaods.com 本期推送的是: 如何正确的使用小程序 每个账号均可小程序[剑3泡泡]搜到,生成专属账号海报! 点击底部[点我卖号]即可拥有! 02教程开始之前,安利 ...

最新文章

  1. cp: /usr/bin/chromedriver: Operation not permitted
  2. 用c语言按颜色对图像进行分类,【leetcode C语言实现】75_颜色分类
  3. 知道邻边和斜边求角度_从数学史角度看数系发展
  4. 评估 | 自动摘要评估
  5. JAVA四种基本排序总结
  6. win7备份工具_win7系统小白一键系统详细教程
  7. 如何成为云原生时代的卓越架构师
  8. TreeView 之间节点拖动 /移动
  9. 用js实现的对css的一些操作方法
  10. 联想服务器TS540装系统蓝屏,联想装win7蓝屏0x000000a5超详细图文解决方案
  11. 《银行业金融机构数据治理指引》解读及解决方案建议
  12. 文本特征提取算法-TF-IDF
  13. html5对锚的更改,HTML5之锚链接
  14. 是时候来了解android7了:通知直接回复
  15. 三星带着Bixby搅局中国市场,还说自己是「一家AI技术公司」
  16. Logo设计没灵感?这7个宝藏网站拯救你的头发
  17. Python基础——继承、多态
  18. 图像处理——图像平滑
  19. 【转】Voip 知识
  20. dz php debug,开发dz插件后台提示错误

热门文章

  1. Alarm Clock C/C++ Version
  2. linux pam limits.so,Linux 无法本地登录解决方法 报错/lib/security/pam_limits.so
  3. 岭南的一艘 “海盗船” 出海时扬起了单机游戏的帆
  4. linux 配置生效的命令,Linux如何让修改的配置生效?
  5. Docker —— 从入门到实践
  6. 百度地图点聚合解决方案
  7. Java 第三阶段增强分析需求,代码实现能力【满汉楼】
  8. Shiro系列(一)——Shiro + Springboot + JWT 整合
  9. Canonical Address
  10. 数据库设计----概念结构设计(概念模型、E—R模型、概念结构设计)