【问题标题】:Google extension how to click a button on the web page?谷歌扩展如何点击网页上的按钮?
【发布时间】:2019-03-08 05:57:54
【问题描述】:

我创建了一个扩展 文件 github.js

jQuery(document).ready(function($) {
  console.log('github.js');
  // btn btn-primary shelf-cta
  var button = $('.btn.btn-primary.shelf-cta');
  console.log('button.html() = ', button.html());
  button.click();
});

文件 popup.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <button id="send_messages">Send Messages</button>

  <script src="libs/jquery-v3.3.1.js"></script>
  <script src="popup.js"></script>
</body>
</html>

文件popup.js

jQuery(document).ready(function($) {
  console.log('popup.js');

  $('#send_messages').click(function(event) {


    console.log('github test');
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
      chrome.tabs.executeScript(
        tabs[0].id,
        {file: "libs/jquery-v3.3.1.js"},
        function(){
          chrome.tabs.executeScript(
            tabs[0].id,
            {file: "github.js"}
          );
        }
      );
    });
  });
});

文件ma​​nifest.json

{
  "name": "test the extension",
  "version": "1.0",
  "description": "Automatically send the message to the opened projects in the browser",
  "permissions": [
    "tabs", 
    "activeTab", 
    "declarativeContent", 
    "storage",
    "<all_urls>"
  ],
  "options_page": "options.html",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "16": "images/get_started16.png",
      "32": "images/get_started32.png",
      "48": "images/get_started48.png",
      "128": "images/get_started128.png"
    }
  },
  "icons": {
    "16": "images/get_started16.png",
    "32": "images/get_started32.png",
    "48": "images/get_started48.png",
    "128": "images/get_started128.png"
  },
  "manifest_version": 2
}

当我去github页面https://github.com/ 有一个“阅读指南”按钮,我想点击它。 我按下扩展 browser_action 按钮,显示一个弹出窗口。当我按下按钮“发送消息”时,什么也没有发生。 我可以到达“阅读指南”按钮并输出标题,但 button.click(); 不起作用。 如何点击按钮或其他链接?

【问题讨论】:

  • 一般来说,可以尝试一下。如果还没有成功,那么人们会按照他们在寻找其他人的方式时找到的方向进行操作。奇怪的是,以下搜索词“chrome 扩展中的单击按钮”导致谷歌向我发送了一个指向此页面的链接:stackoverflow.com/questions/26390322/…
  • 我检查了您链接上的代码,它可以正常工作。但是当我添加 jquery 时它不起作用
  • (Un?)幸运的是,我有幸避开了任何需要使用 jQuery 的项目。结果,我没有学习它,也没有对它产生任何兴趣,而且似乎无法提供任何进一步的帮助。对不起!

标签: javascript google-chrome-extension


【解决方案1】:

所以jquery中的点击需要使用这段代码来完成

button.get(0).click();

不像我做的

button.click();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-03-27
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 2015-09-28
    • 2019-11-13
    • 1970-01-01
    相关资源
    最近更新 更多