【问题标题】:Execute script after click in popup.html (chrome extension)点击popup.html(chrome扩展)后执行脚本
【发布时间】:2013-12-24 17:15:37
【问题描述】:

当我单击popup.html 中的按钮时,我试图在页面上执行 javascript。我尝试使用这样的方式:

background.js 中:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo){
    if(changeInfo.status == "loading") {
        insert(tabId);
    }
});
function insert(tabId) {
    chrome.tabs.get(tabId, function(tab) {
        $('button').click(function() {
            chrome.tabs.executeScript(tab.id, {file: 'js/alert.js'});
        });
    });
}

Alert.js 只包含一个字符串:alert('works');

警报只是一个例子。在用户点击一个按钮 im popup.html 后,真正的脚本应该使用打开的选项卡进行一些 DOM 操作。

【问题讨论】:

  • 您似乎搞砸了一些概念(例如弹出窗口、背景页面等)。发布您的清单也会有所帮助。
  • 提交的background.js 不符合您的要求,而且似乎没用。在您的内容脚本中,将警报放入一个函数中,例如“handleButtonClick”。确保内容脚本已在清单中注册。然后在 popup.html 中调用你的函数:chrome.tabs.executeScript({code: 'handleButtonClick()'});.

标签: javascript jquery google-chrome google-chrome-extension


【解决方案1】:

我为您的需要编写了一个演示。

https://gist.github.com/greatghoul/8120275


alert.js

alert('hello ' + document.location.href);

background.js

// empty file, but needed

图标.png

manifest.json

{
  "manifest_version": 2,

  "name": "Click to execute",
  "description": "Execute script after click in popup.html (chrome extension) http://stackoverflow.com/questions/20764517/execute-script-after-click-in-popup-html-chrome-extension.",
  "version": "1.0",

  "icons": {
    "48": "icon.png"
  },

  "permissions": [
    "tabs", "<all_urls>"
  ],

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

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

popup.html

<!DOCTYPE html>
<html>
  <body style="width: 300px">
    Open <a href="http://stackoverflow.com" target="_blank">this page</a> and then 
    <button id="clickme">click me</button>
    <script type="text/javascript" src="popup.js"></script>
  </body>
</html>

popup.js

// var app = chrome.runtime.getBackgroundPage();

function hello() {
  chrome.tabs.executeScript({
    file: 'alert.js'
  }); 
}

document.getElementById('clickme').addEventListener('click', hello);

【讨论】:

  • 我为你的努力投了赞成票,但你添加空的 background.js 是为了什么?
  • 原谅我,background.js 不是必须的。
  • @VladGolubev 没有这样的 api,但是你可以使用纯 javascript document.createElement('tag_name'); 或 jQuery 来插入 html。这很容易。
  • 虽然理论上可以回答这个问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。将您的 Gist 的内容复制到此答案中将是微不足道的。但是,您没有在要点中说明代码的许可条款。因此,不清楚是否允许我这样做。
  • 这个脚本将在哪里执行?在 popup.html 上?还是活动网页(如 contentscript)?
【解决方案2】:

您也可以使用消息传递:

在 popup.js 中

document.getElementById("clicked-btn").addEventListener("click", function(e) {
    chrome.runtime.sendMessage({'myPopupIsOpen': true});
});

在 background.js 中

chrome.runtime.onMessage.addListener(function(message, sender) {
      if(!message.myPopupIsOpen) return;

      // Do your stuff
});

未经测试但应该可以工作,有关Messaging的更多信息。

【讨论】:

  • 这是一种更好的方法,而不是像@greatghoul 的回答那样运行原始脚本
  • 理论上可能会更好,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 2012-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
相关资源
最近更新 更多