简体   繁体   English

如何单击 Chrome 扩展程序中的按钮

[英]How to click on a button in Chrome Extension

I am just trying to set up a simple chrome extension that will just click on an element once the extension button is clicked on.我只是想设置一个简单的 chrome 扩展,一旦点击扩展按钮,它就会点击一个元素。

I have researched this a bit, and I can't find a simple answer that says how to click, everyone else has very elaborate code that I can't understand and don't know if it's necessary or not.我对此进行了一些研究,但找不到一个简单的答案来说明如何单击,其他所有人都有非常复杂的代码,我无法理解,也不知道是否有必要。 What I mean is, whenever I search for just "click" I find a question that is much more advanced than my level.我的意思是,每当我搜索“点击”时,我会发现一个比我的水平高得多的问题。

(I am about to make a lot of $ off of this extension, so please will you help a brother out ;) (我即将从这个扩展中赚很多钱,所以请你帮助兄弟;)

Using what I have seen I have scraped together the code:使用我所看到的,我将代码拼凑在一起:

popup.js: popup.js:

var evt = document.createEvent ("HTMLEvents");
evt.initEvent ("click", true, true);

document.getElementById('product-addtocart-button').dispatchEvent (evt);

manifest.Json:清单.json:

{
  "manifest_version": 2,

  "name": "Shoe BOT",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "https://shop.adidas.ae/en/"
  ]
}

popup.html: popup.html:

<!doctype html>
<!--
 This page is shown when the extension button is clicked, because the
 "browser_action" field in manifest.json contains the "default_popup" key with
 value "popup.html".
 -->
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {

      }
      #status {

      }
    </style>

    <!--
      - JavaScript and HTML must be in separate files: see our Content Security
      - Policy documentation[1] for details and explanation.
      -
      - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
     -->
    <script src="popup.js"></script>
  </head>
  <body>
    <!--<div id="status"></div>
    <img id="image-result" hidden>-->
  </body>
</html>

It is not clear why you are using document.createEvent() . 目前尚不清楚为什么要使用document.createEvent() That interface is deprecated. 该接口已弃用。 To create events, you should be using event constructors . 要创建事件,您应该使用事件构造函数 However, for a generic click event on an HTML element, you can just use the click() method without having to actually construct the event. 但是,对于HTML元素上的常规click事件 ,您可以仅使用click()方法,而不必实际构造该事件。

A simple, complete Chrome extension which injects a content script to click the button with id="product-addtocart-button" when you click on a browser_action button would be: 一个简单,完整的Chrome扩展程序可以是:当您单击browser_action按钮时,会注入内容脚本以单击带有id="product-addtocart-button"按钮:

background.js : background.js

chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(tab.id,{
        code: "document.getElementById('product-addtocart-button').click();"
    });
});

manifest.json : manifest.json

{
    "description": "Click a button with ID=product-addtocart-button",
    "manifest_version": 2,
    "name": "click-product-addtocart-button",
    "version": "0.1",

    "permissions": [
        "activeTab"
    ],

    "background": {
        "scripts": [
            "background.js"
        ]
    },

    "browser_action": {
        "default_icon": {
            "32": "myIcon.png"
        },
        "default_title": "Click product-addtocart-button"
    }
}

With the new manifest version (V3) the page_action and browser_action were unified into one single action :在新的清单版本(V3)中, page_actionbrowser_action被统一为一个action

  "action": {
    "title": { 'This is a popup tip!'}
}

You just avoid declaring any default_popup and in background.js you write:您只需避免声明任何default_popup并在background.js中编写:

    chrome.action.onClicked.addListener(
        async function(tab) {
          // what you want to do when click the extension button
    });

Incredibly easy !非常容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从点击按钮运行 chrome 扩展程序? - How to run a chrome extension from a click button? 收听 chrome 扩展中的按钮单击 - Listen to a button click in a chrome extension 当我获得带有 chrome 扩展名的按钮的 DOM 时,如何进行“点击”? - How to make a "click" as I got the DOM of the button with chrome extension? 如何在单击按钮时将html扩展为带有chrome扩展名的网页 - How to inject html into webpage with chrome extension on button click Chrome扩展程序如何响应按钮在页面中的点击? - How can a Chrome extension respond to a button click in a page? 如何单击带有chrome扩展名的这种按钮(给定的DOM)? - How to click this kind of button (the DOM given) with chrome extension? 如何通过将侦听器添加到“窗口”来从Chrome扩展程序单击网页上的按钮? - How to click a button on a webpage from Chrome extension by adding a listener to `window`? 如何在单击按钮时调整 chrome 弹出扩展的大小以使其变大 'x'% - How to resize chrome popup extension on button click to make it larger by 'x'% Chrome扩展程序-如何使用按钮单击功能执行javascript文件? - Chrome Extension - How to execute a javascript file with button click function? 如何以编程方式单击带有Chrome扩展程序的Google文档中的按钮? - How can I programmatically click a button in Google Docs with a Chrome Extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM