只需 5 个步骤即可创建并发布您的第一个 Chrome 扩展程序。

2025-06-07

只需 5 个步骤即可创建并发布您的第一个 Chrome 扩展程序。

什么是 Chrome 扩展程序?

扩展程序是用于定制浏览体验的小型软件程序。它们使用户能够根据个人需求或偏好定制 Chrome 的功能和行为。它们基于 HTML、JavaScript 和 CSS 等网络技术构建。——  Chrome 开发者

入门

在直接深入开发部分之前,首先退一步思考一下,你想要创造什么?

今天,我们正在创建一个扩展程序,每当用户切换到新标签时,它就会显示新的引文。

步骤 1:告诉 Chrome 你的扩展程序

我们必须创建一个 JSON 格式的清单文件,其中包含扩展的详细信息,如扩展的名称、描述等。

对于此扩展,我们需要像activeTab 这样的权限。

打开一个名为manifest.json的文件

{    
  "manifest_version": 2,

  "name": "QuoteThat",    
  "description": "An Extension which show quotes whenever user switch to new tab. It will work offline and change quote in every 60 seconds.",    
  "version": "1.0.0",    
  "chrome_url_overrides" : {  
    "newtab": "newtab.html"    
  },  
  "browser_action":{      
    "default_icon": "icon.png"    
  },  
  "permissions": ["activeTab"]  
}
Enter fullscreen mode Exit fullscreen mode

正如您在“ newtab ”中看到的,我们希望每当用户切换到新选项卡时都会呈现_newtab.html。

步骤2:制作HTML文件

打开newtab.html

<!DOCTYPE html>  
<html>  
<head>  
  <title>New Tab</title>  
</head>  
<body>  
  <blockquote>  
    <center>  
      <p id="quote"></p>  
      <footer>   
        <cite id="author"></cite>  
      </footer>  
    </center>  
  </blockquote>  
</body>  
</html>
Enter fullscreen mode Exit fullscreen mode

添加一些CSS以使您的页面更美观。

<style>  
    body   
    {  
      background-image: url("back.jpg");   
      -webkit-background-size: cover;  
      -moz-background-size: cover;  
      -o-background-size: cover;  
      background-size: cover;  
      position: absolute;  
      width: 70%;  
      top: 25%;  
      left: 0;  
      right: 0;  
      margin: auto;  
    }  
    p   
    {  
     font-size:35px;  
     color: white;  
    }  
    cite   
    {  
      font-size:25px;  
      color: yellow;  
    }  
</style>
Enter fullscreen mode Exit fullscreen mode

因此,你的newtab.html将如下所示

<!DOCTYPE html>  
<html>  
<head>  
  <title>New Tab</title>  
  <style>  
    body   
    {  
      background-image: url("back.jpg");   
      -webkit-background-size: cover;  
      -moz-background-size: cover;  
      -o-background-size: cover;  
      background-size: cover;  
      position: absolute;  
      width: 70%;  
      top: 25%;  
      left: 0;  
      right: 0;  
      margin: auto;  
    }  
    p   
    {  
     font-size:35px;  
     color: white;  
    }  
    cite   
    {  
      font-size:25px;  
      color: yellow;  
    }  
  </style>  
</head>  
<body>  
  <blockquote>  
    <center>  
      <p id="quote"></p>  
      <footer>   
        <cite id="author"></cite>  
      </footer>  
    </center>  
  </blockquote>  
  <script src="jquery.min.js"></script>  
  <script src="javascript.js"></script>  
</body>  
</html>
Enter fullscreen mode Exit fullscreen mode

现在,正如您所看到的,我们添加了一个 Javascript 文件,但在此之前,让我们看一个包含将在新选项卡中显示的引号的 JSON 文件。

quotes.json

[  
 [  
  "William James",  
  " Act as if what you do makes a difference. It does."  
 ],  
 [  
  "Bill Cosby",  
  " Decide that you want it more than you are afraid of it."  
 ],  
 [  
  "Judy Collins",  
  " I think people who are creative are the luckiest people on earth. I know that there are no shortcuts, but you must keep your faith in something Greater than You, and keep doing what you love. Do what you love, and you will find the way to get it out to the world."  
 ],  
 [  
  "Jessica Savitch",  
  " No matter how many goals you have achieved, you must set your sights on a higher one."  
 ],
Enter fullscreen mode Exit fullscreen mode

可以看到,json 文件中有作者和引言。因此,我们将显示引言及其作者。

现在,让我们编写javascript.js

function Quote(callback)   
{  
  $.getJSON('quotes.json',function(data)   
  {  
    var rN=Math.round(Math.random()*(data.length-1));  
    var author=data[rN][0];  
    var quote=data[rN][1];  
    callback(quote,author);  
  });  
};  
function changeQuote()   
{  
  callback=function(quote, author)   
  {  
    $("p#quote,cite#author").fadeOut(function()   
    {  
      $("p#quote").text(quote).fadeIn(function()   
      {  
        $("cite#author").text(author).fadeIn();  
      });  
    });  
  };  
  Quote(callback);  
};  
$(window).load(function()   
{  
  changeQuote();  
  setInterval(changeQuote,60000);  
});
Enter fullscreen mode Exit fullscreen mode

Quote() 函数将从quote.json 文件中随机选择数据,并使用引文及其作者进行回调。

函数changeQuote()将在每次调用时更改报价。$(window).load(function(){}) 将每隔一段时间调用一次 changeQuote()。

步骤 3:查看扩展程序是否正常运行

转到 Google Chrome -> 右上角(三个点)-> 更多工具 -> 扩展程序。

然后打开开发者选项并点击*加载解压文件*

您将看到您的扩展

现在,打开一个新标签页来查看您的扩展程序是否正常工作

步骤 5:发布

转到此链接并使用您的 Gmail 帐户登录,然后单击“添加新项目”

注意:您必须支付 5.00 美元来验证您的帐户

上传文件后,您将看到一个表单,您需要在其中添加有关扩展程序的信息,例如添加图标、详细描述等等。您可以在Chrome 网上应用店中查看您的扩展程序。

您可以在Github上看到完整的代码

文章来源:https://dev.to/sahilrajput/create-and-publish-your-first-chrome-extension-in-just-5-steps-3c3n
PREV
使用 Python 将文件从笔记本电脑传输到手机
NEXT
5 分钟内构建您的第一个聊天机器人