谷歌插件编写

目录

manifest.json

{
    "manifest_version": 3,
    "name": "Floating Ball",
    "version": "1.0",
    "description": "A floating ball on the right side of the webpage.",
    "permissions": ["activeTab"],
    "action": {
      "default_popup": "popup.html",
      "default_icon": {
        "16": "1.jpg"
      }
    },
    "content_scripts": [
      {
        "matches": ["<all_urls>"],
        "js": ["content.js"]
      }
    ]
  }
  

popup.html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>小宠物插件</title>
    <style>
        #root {
            width: 200px;
        }
    </style>
</head>

<body>
    <div id="root">
        <div>悬浮球插件</div>
        <div>Copyright (c) 2024, Qvfan</div>
    </div>
</body>

</html>

content.js

const floatingBall = document.createElement('div');
const smallBox = document.createElement('div');

Object.assign(floatingBall.style, {
    position: 'fixed',
    right: '20px',
    bottom: '200px',
    width: '50px',
    height: '50px',
    background: 'url(https://pic4.zhimg.com/80/v2-3628c58a24939a53ffd00bb55ccaa7c3_1440w.webp) no-repeat',
    backgroundSize: 'cover',
    borderRadius: '50%',
    boxShadow: '0 0 10px rgba(0,0,0,0.5)',
    zIndex: '1000',
    cursor: 'pointer'
});

Object.assign(smallBox.style, {
    position: 'absolute',
    top: '-270px',
    left: '-298px',
    width: '300px',
    height: '300px',
    backgroundColor: '#fafafa',
    boxShadow: '0 0 10px rgba(0,0,0,0.5)',
    display: 'none' // 初始时隐藏
});

document.body.appendChild(floatingBall);

floatingBall.appendChild(smallBox);

const showSmallBox = () => {
    smallBox.style.display = 'block';
};

const hideSmallBox = () => {
    smallBox.style.display = 'none';
};


floatingBall.addEventListener('mouseover', showSmallBox);
floatingBall.addEventListener('mouseout', hideSmallBox);

smallBox.addEventListener('mouseover', showSmallBox);
smallBox.addEventListener('mouseout', hideSmallBox);



floatingBall.addEventListener('click', (e) => {
    alert('屈凡的悬浮球被点击了!' + JSON.stringify(e));
});

直接打开

效果

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用ReactJS编写谷歌件,您需要使用Chrome扩展程序API。以下是一些步骤: 1.创建一个新的Chrome扩展程序项目,并在其中创建一个名为popup.html的文件。 2.在popup.html文件中,您需要添加一个div元素,该元素将用于呈现您的React组件。您还需要在文件中添加一个script标记,该标记将引用您的React组件的JavaScript文件。 3.创建一个名为popup.js的JavaScript文件,并在其中编写您的React组件。您可以使用JSX编写组件,因为Chrome扩展程序支持JSX。 4.在popup.js文件中,您需要使用Chrome扩展程序API来与浏览器进行交互。例如,您可以使用chrome.tabs API来获取当前选项卡的URL。 5.将您的React组件呈现到popup.html文件中的div元素中。 以下是一个简单的示例,演示如何使用ReactJS编写Chrome扩展程序: 1.在Chrome扩展程序项目中创建一个名为popup.html的文件,并添加以下内容: ```html <!DOCTYPE html> <html> <head> <title>My Extension</title> </head> <body> <div id="root"></div> <script src="popup.js"></script> </body> </html> ``` 2.创建一个名为popup.js的文件,并添加以下内容: ```javascript import React from 'react'; import ReactDOM from 'react-dom'; class MyComponent extends React.Component { constructor(props) { super(props); this.state = { url: '' }; } componentDidMount() { chrome.tabs.query({ active: true, currentWindow: true }, tabs => { this.setState({ url: tabs[0].url }); }); } render() { return <div>Current URL: {this.state.url}</div>; } } ReactDOM.render(<MyComponent />, document.getElementById('root')); ``` 3.在Chrome扩展程序项目中加载您的扩展程序,并单击浏览器工具栏中的扩展程序图标。您应该会看到一个弹出窗口,其中包含当前选项卡的URL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值