chrome扩展crx构建

“Sit up straight!” my mom used to yell at me when I am fixated on my laptop for too long.

“坐直了!” 当我将笔记本电脑注视时间过长时,我妈妈曾经对我大喊大叫。

Ten years later, when I am writing code instead of playing video games, my poor posture has come back to bite me.

十年后,当我写代码而不是玩视频游戏时,我糟糕的姿势又重新咬了我。

Therefore, I decided to take the matter into my own hands. I could watch some YouTube videos and do some posture exercises. But I am a coder, so I will code my problem.

因此,我决定亲自处理此事。 我可以看一些YouTube视频并做一些姿势练习。 但是我是一名编码人员,所以我将为我的问题编码。

That’s how Healthy Spine was born. It watches your posture and reminds you when you slouch— just like my mom back in the day.

这就是健康脊柱的诞生。 它可以观察您的姿势并在您不便时提醒您-就像我一天中的妈妈一样。

先决条件 (Prerequisites)

  • Basic knowledge of HTML, CSS, and JavaScriptHTML,CSS和JavaScript的基本知识
  • Access to a Chrome Web Store developer account访问Chrome Web Store开发人员帐户

This is a summarized version of what I learned from creating my first Chrome extension. If you need a full step-by-step tutorial, watch Chrome’s Getting Started tutorial.

这是我从创建第一个Chrome扩展程序中学到的摘要。 如果您需要完整的分步教程,请观看Chrome的入门教程 。

Chrome扩展程序与Chrome应用程序 (Chrome Extension vs. Chrome App)

Chrome apps are a more powerful version of Chrome extensions.

Chrome应用是功能更强大的Chrome扩展程序。

However, Google has decided to sunset Chrome apps altogether this year.

但是,Google已决定今年完全停用Chrome应用。

Chromium blog post, January 15, 2020 Chromium博客文章,2020年1月15日

However, in their documentation, they still have the Chrome Apps API, which will give you false hope that you could still build a Chrome app.

但是,在他们的文档中,他们仍然拥有Chrome Apps API,这会给您错误的希望,您仍然可以构建Chrome应用程序。

Make sure that you look at the extension APIs for Chrome extension documentation.

确保您查看了Chrome扩展程序文档的扩展程序API。

典型的Chrome扩展程序的结构 (The Structure of a Typical Chrome Extension)

The image folder is a place to store all your icons.

image文件夹是存储所有图标的位置。

options files are ways for you to define how a user can customize the Chrome extension once they download it.

options文件是您定义用户下载Chrome扩展程序后如何对其进行自定义的方式。

However, as of now (2020/8/9), the options are hidden deep within the settings. I doubt any user will realize that they’re even there.

但是,截至目前(2020/8/9),选项已隐藏在设置的深处。 我怀疑任何用户都不会意识到他们在那里。

For other parts of the code, it’s broken down into four parts:

对于代码的其他部分,它分为四个部分:

  1. Background背景
  2. Popup弹出
  3. Content (not in the Getting Started project folder)内容(不在“入门”项目文件夹中)
  4. Setting设置

后台脚本 (Background Script)

As the name suggests, it runs in the background when the user installs the Chrome extension.

顾名思义,当用户安装Chrome扩展程序时,它将在后台运行。

To see the debug console for the background script, you can click on “Inspect views background page” in chrome://extensions.

要查看后台脚本的调试控制台,您可以点击chrome:// extensions中的“检查视图背景页面”。

What kind of code should you put in the background script?

您应该在后台脚本中放入哪种代码?

For some functionalities, like notification, you might want to trigger it in the background script. Or you might want a timer to run in the background.

对于某些功能,例如通知,您可能希望在后台脚本中触发它。 或者,您可能希望计时器在后台运行。

The most important code to be written in background.js is chrome.runtime.

background.js编写的最重要的代码是chrome.runtime

运行时事件 (Events for runtime)

onInstalled:

onInstalled

chrome.runtime.onInstalled.addListener(function() {  // Code to execute once the extension is installed});

onStartup:

onStartup

chrome.runtime.onStartup.addListener(function() {// Code to execute once Google Chrome is opened});

For background to communicate with the popup or content scripts, you need to use onMessage and to receive messages.

为了使背景与弹出脚本或内容脚本进行通信,您需要使用onMessage并接收消息。

In background.js, you would listen to the messages as in the code below.

background.js ,您将按照以下代码收听消息。

var runtimePort;
chrome.runtime.onConnect.addListener(function(port) {runtimePort = port;runtimePort.onMessage.addListener(function(message) {// Events from either popup or content script});
});

In the popup or content scripts, you can connect with:

在弹出或内容脚本中,您可以连接:

const runtimePort = chrome.runtime.connect({name: location.href.replace(/\/|:|#|\?|\$|\^|%|\.|`|~|!|\+|@|\[|\||]|\|*. /g, '').split('\n').join('').split('\r').join('')});

To send an event (message), do postMessage:

要发送事件(消息),请执行postMessage

runtimePort.postMessage({  message: ''});

弹出脚本 (Popup Script)

Popup is the menu that shows up when you click on the extension’s icon.

单击扩展程序的图标时将显示弹出菜单。

It doesn’t have to be a menu. You can adjust the HTML, CSS, and JavaScript to be anything you want.

它不必是菜单。 您可以将HTML,CSS和JavaScript调整为所需的任何值。

Therefore, the popup is just like a website. You can use any framework or no frameworks.

因此,弹出窗口就像一个网站。 您可以使用任何框架,也可以不使用任何框架。

Here’s an example of popup.html:

这是popup.html的示例:

<!DOCTYPE html><html><head><style>button {height: 30px;width: 30px;outline: none;}</style>
</head><body><button id="changeColor"></button><script src="popup.js"></script></body>
</html>

See? It looks just like an ordinary website’s HTML.

看到? 它看起来就像普通网站HTML。

Below is an example of popup.js code:

以下是popup.js代码的示例:

'use strict';let changeColor = document.getElementById('changeColor');
// chrome.storage can be accessed in all scripts(background, content, popup)
chrome.storage.sync.get('color', function(data) {changeColor.style.backgroundColor = data.color;changeColor.setAttribute('value', data.color);
});
changeColor.onclick = function(element) {let color = element.target.value;// chrome.tabs let you access a specific tabchrome.tabs.query({active: true, currentWindow: true},        function(tabs) {chrome.tabs.executeScript(tabs[0].id,{code: 'document.body.style.backgroundColor = "' + color +'";'});});
};

In Healthy Spine, I used p5js and ML5js in order to achieve posture detection.

在Healthy Spine中,我使用了p5js和ML5js来实现姿势检测。

内容脚本 (Content Script)

The content script lets you manipulate the webpage that the user is currently using.

内容脚本使您可以操纵用户当前正在使用的网页。

For example, that AdBlocker extension probably uses a content script to detect ad-related popups and prevent them from executing in JavaScript.

例如,该AdBlocker扩展程序可能使用内容脚本来检测与广告相关的弹出窗口,并阻止它们在JavaScript中执行。

Similar to a popup script, the content script contains HTML, CSS, and JavaScript.

类似于弹出脚本,内容脚本包含HTML,CSS和JavaScript。

However, in manifest.json remember to add in your file paths.

但是,请记住在manifest.json中添加文件路径。

{"name": "My extension",..."content_scripts": [{"matches": ["http://*.nytimes.com/*"],"exclude_globs": ["*science*"],"js": ["contentScript.js"]}],...
}

matchesand exlude_globs will specify which URL your content script executes. For the list of all the patterns, see Chrome’s Match Patterns documentation.

matchesexlude_globs将指定您的内容脚本执行哪个URL。 有关所有模式的列表,请参阅Chrome的“ 匹配模式”文档 。

设定值 (Settings)

All the important settings are in the manifest.json.

所有重要的设置都在manifest.json

It does a few things:

它做一些事情:

  1. It tells Chrome Web Store what your application is and what it does.它告诉Chrome Web Store您的应用程序是什么以及它做什么。
  2. It defines where to look for the background, popup, or content script, etc.它定义了在哪里寻找背景,弹出窗口或内容脚本等。
  3. It defines what the icon for the extension is.它定义了扩展程序的图标。
  4. It defines what permission you need from users.它定义了您需要来自用户的权限。

It also has other rules as well, but those will depend on the individual extension’s use case.

它还具有其他规则,但是这些规则将取决于各个扩展的用例。

Note that the manifest.json file below doesn’t have a content script because my extension did not need to access the user’s webpage.

请注意,下面的manifest.json文件没有内容脚本,因为我的扩展程序不需要访问用户的网页。

注意 (Note)

One thing that tripped me up was the difference between page_action and browser_action.

使我page_action一件事是page_actionbrowser_action之间的区别。

If you specify your popup in page_action, your extension icon will only show up when a user opens a link.

如果您在page_action指定弹出page_action ,则扩展图标仅在用户打开链接时显示。

Using browser_action, the icon will show up when Chrome is opened, which was the action I wanted.

使用browser_action ,打开Chrome时将显示该图标,这是我想要的操作。

提交到Chrome网上应用店 (Submit to Chrome Web Store)

First, you need to pay $5 to register for a Chrome Web Store developer account. (I wonder if Google really needs my $5?)

首先,您需要支付5美元才能注册Chrome Web Store开发者帐户。 (我想知道Google是否真的需要我的5美元?)

https://developer.chrome.com/webstore/register

https://developer.chrome.com/webstore/register

After you register, you need to use zip to bundle your extension’s files and upload them to their dashboard.

注册后,您需要使用zip捆绑扩展程序的文件并将其上传到其仪表板。

It will ask you to fill in some information about your extension, and it adds some marketing images.

它将要求您填写有关扩展的一些信息,并添加一些营销图像。

One important thing is to make sure you only request for the permission that you use in the extension.

重要的一件事是确保仅请求扩展中使用的权限。

My app was rejected three times because I thought using local storage required the permission “store.”

我的应用被拒绝了三次,因为我认为使用本地存储需要权限“存储”。

结论 (Conclusion)

Chrome extensions are a great way to build something practical to improve how you work or how other people work.

Chrome扩展程序是构建实用工具以改善您的工作方式或其他人的工作方式的好方法。

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/how-to-build-your-first-chrome-extension-8abdee9a4365

chrome扩展crx构建


http://www.taodudu.cc/news/show-5813676.html

相关文章:

  • Winrar阻止弹窗分析(鼓捣版)
  • 微信小程序-个人总结
  • 阻止android应用调用_如何在Android上设置应用时间限制和阻止应用
  • 【安装极致系统时出现的问题记录】解决SQL Server 阻止了对组件Ad Hoc Distributed Queries访问的方法
  • Error 1275 - 此驱动程序被阻止加载
  • Flash中打开链接绕过弹出窗口阻拦程序的方法
  • 小米和360随身wifi在linux(Ubuntu)下作无线网卡教程
  • 开发支付宝小程序问题一:很抱歉,系统监测到你的支付宝账号有异常,入驻失败
  • 刷脸系统将消费者面部信息与个人账户关联
  • Java web集成支付宝电脑支付接口(沙箱环境)
  • OPPOK9和小米EA552022对比哪个好
  • OPPO Find N和小米MIX FOLD 哪个好
  • 差价500,Find X3和小米11哪个好?对比后答案明显
  • 小米oppo都在适配鸿蒙,针对华为鸿蒙系统,小米、OPPO相继表态,魅族的态度截然相反...
  • 智慧水务系统解决核心问题
  • 中国再生金属行业占有率分析与营销战略研究报告2022-2028年
  • 国际可再生能源机构:区块链是变革可再生能源领域的30项重要创新之一
  • Clonezilla live 再生龙还原系统
  • 计算机网络和因特网(网络核心和接入网)
  • 中国再生水行业运行格局及投资战略咨询报告2022-2027年新版
  • 中国再生金属行业运行现状与发展趋势研究报告2022版
  • Openshift核心概念
  • 区块链助力可再生能源走上新赛道
  • 可再生能源咨询市场现状研究分析报告-
  • 可再生能源证书市场现状研究分析-
  • 2021-2025年中国可再生能源核心材料行业市场供需与战略研究报告
  • 2022-2028全球与中国汽车可再生材料市场现状及未来发展趋势
  • 【408笔记】计算机组成原理 第三章 存储系统
  • 社交市场如此成熟,为何陌陌还能“逆生长”?
  • 年入百万的女主播们,能撑起陌陌的未来吗?

chrome扩展crx构建_如何构建您的第一个Chrome扩展程序相关推荐

  1. 本地构建和自动化构建_如何构建最强大,最安全的家庭自动化系统

    本地构建和自动化构建 by Amir Off 由Amir Off 如何构建最强大,最安全的家庭自动化系统 (How to build the most robust and secure home a ...

  2. 数字画像构建_想构建数字产品? 首先问自己这四个问题

    数字画像构建 我盘腿坐在星巴克上,笔记本电脑放在腿上. 我出去尝试寻找平静的环境来专注于作为前端产品开发人员正在完成的工作申请. 任务很简单:创建一个演示应用程序,该应用程序连接到API(Foursq ...

  3. python的简单程序代码_小白学编程?从一个简单的程序开始学习Python编程

    笔者思虑再三还是决定选择图文(因为百家的视频发布画质真不怎么样[囧]). 笔者学习编程的时间也挺长的,因为业余,因为时间不多,各种原因,自学编程的路特别难走.然后笔者发现,自己能为小白贡献一些力量,然 ...

  4. 扩展修改ubuntu 13.04 用c快速编写一个php扩展

    首先声明,我是一个菜鸟.一下文章中现出技术误导情况盖不负责 本文通过非常倏地的方法解讲了如何作制一个PHP 5.4 环境的扩展(PHP Extension) 希望够能在图文的方法下让想倏地学习的友人解 ...

  5. git32位服务器构建_如何构建自己的Git服务器

    git32位服务器构建 读: 第1部分:什么是Git? 第2部分:Git入门 第3部分:创建第一个Git存储库 第4部分:如何在Git中还原旧文件版本 第5部分:3个用于Git的图形工具 第6部分:如 ...

  6. python项目构建_通过构建4个项目来学习Python网络

    python项目构建 The Python programming language is very capable when it comes to networking. We've releas ...

  7. angular 模块构建_通过构建全栈应用程序学习Angular 6

    angular 模块构建 Angular 6 is out! The new features include better performance, new powerful CLI additio ...

  8. 安卓手机如何打开.crx文件_如何在安卓手机上使用Chrome插件

    原标题:如何在安卓手机上使用Chrome插件 目前世面上的手机浏览器大都以chrome为核心,奇怪的是chrome在桌面版上的神技-"插件",在手机上却不支持.于是乎国内手机浏览器 ...

  9. python 构建_通过构建互动游戏来教孩子Python

    python 构建 Python已作为一种出色的初学者编程语言而享有盛誉. 但是,从哪里开始呢? 我最喜欢的使人们对编程感兴趣的方法之一就是编写游戏. PursuedPyBear (ppb)是为教学而 ...

最新文章

  1. 员工因公司而加入,却因主管而离开
  2. idea 自动生成构造以及get、set方法
  3. vue常用语法 渲染数据
  4. Linux系统默默改变了人类世界的生活方式
  5. cmenu 隐藏子项中的一个子项_QML中的模型-视图-代理
  6. 第三周课程总结实验报告
  7. 【OpenCV 例程200篇】37. 图像的灰度化处理和二值化处理
  8. CCF201703-2 学生排队(100分)
  9. 2021-06-28操作表单
  10. 建模大师怎么安装到revit中_用协同大师完成Revit协同工作的教程详解
  11. 微型计算机原理聂伟荣,微型计算机原理与应用 聂伟荣 第十章 串行通信技术 课件.pdf...
  12. 盒仔机器人_DFROBOT SEN0240 肌电传感器 OYMotion 产品资料 使用教程
  13. error LNK2001的一些原因
  14. java 米与厘米 转换_米转码换算(米与码的换算关系)
  15. python 密码输入显示星号_[145]python实现控制台密码星号输入
  16. 【信息学奥赛一本通】1114:白细胞计数
  17. JSON.stringfy()详解
  18. SSM项目之注册页面知识点整理
  19. IDEA高效使用技巧--->IDEA批量修改变量快捷键和全局搜索键
  20. 软件测试分类-按照开发阶段划分

热门文章

  1. 定时发送短信任务怎么写java代码
  2. java app token 失效_请求时token过期自动刷新token操作
  3. matlab中的伽马函数,伽马函数(Γ(x)伽马函数公式)
  4. idea 导入新项目老是缺少依赖
  5. 网络编程套接字(上篇)UDP实现简易多人聊天室
  6. 快照测试_什么是快照测试,并且在PHP中可行?
  7. 一个优秀的程序员应该具备哪些技能和修养?
  8. linux查看哪个网卡插着网线,(笔记)Linux下检测网卡与网线连接状态
  9. 纯CSS实现的文字效果
  10. Ambari.properties文件找不到