简体   繁体   English

Chrome扩展程序| 使用类似消息的Chrome API发送“ this”和“ this” id值

[英]Chrome Extension | Sending “this” and “this” id value using a Chrome API like message

Stackoverflow community, hello. Stackoverflow社区,您好。 I am here asking for some help with figuring out how to send "this" with the id of "this" using the message API for Google chrome extensions. 我在这里寻求一些帮助,搞清楚如何发送"this"the id of "this"使用谷歌Chrome浏览器扩展的消息API。

Relevant html code used (popup.html) 使用的相关html代码(popup.html)

<p class="field switch bpos">
    <label class="cb-enable" id="id1"><span>yes</span></label>
    <label class="cb-disable selected"><span>no</span></label>
    <input type="checkbox" id="checkbox" class="checkbox">
</p>

Listener.js (external script) (placed within script tags at the of popup.html) Listener.js(外部脚本)(放置在popup.html的脚本标签中)

$( ".cb-enable" ).click(function() {
chrome.tabs.executeScript(null, {file:"script.js"});
});

Script.js (run from {file:"script.js"} ) Script.js(从{file:“ script.js”}运行)

function check(){
if ( $( this ).is('#id1')) {function1(); }
else if ( $( this ).is('#id2')) {function2(); }
else{alert("id not found");}
}
check();

Being relatively new to the chrome extension APIs, the use of file and sending information from one script to the other using the messaging API has got me rather confused, and am hoping one of you could help me a bit with how exactly "this" with the ID value of this was sent from one file to the other. 在chrome扩展API方面相对较新,使用文件以及使用消息传递API从一个脚本向另一个脚本发送信息的使用使我感到非常困惑,并且希望你们中的一个可以对我的"this"有何帮助the ID value of this已从一个文件发送到另一个文件。

If anyone could help even in the slightest of this specific request, your help would be incredibly appreciated. 如果有人能在这个特殊要求中提供一点帮助,您的帮助将非常感谢。

First of all, you must take in mind, that DOM tree of popup.html and DOM tree accessible from your injected script.js are different. 首先,必须记住,popup.html的DOM树和从注入的script.js可访问的DOM树是不同的。 So on if you pass jQuery event to script.js, it would not work proper. 因此,如果您将jQuery事件传递给script.js,它将无法正常工作。 Instead of it, you can pass target id to your script.js. 代替它,您可以将目标ID传递给script.js。

Secondly, why you inject your script.js to the page every click? 其次,为什么每次单击都将script.js注入页面? You may do it once. 您可以做一次。 And then send messages to the page. 然后将消息发送到页面。

Thirdly, you work with DOM in your Listener.js, which may be not ready, I recommend to bind it to document.ready: 第三,您可能尚未准备好在Listener.js中使用DOM,我建议将其绑定到document.ready:

It may be like: 它可能像:

Listener.js : Listener.js

$(document).ready(function(){
    chrome.tabs.executeScript(null, {file:"script.js"});
    $( ".cb-enable" ).click(function(evt) {
        target=$(this).attr("id")
        chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
            chrome.tabs.sendMessage(tabs[0].id, target, function(response) {
            });
        });
    });
});

script.js : script.js

if(!window.init)
{
  window.init=true
  chrome.runtime.onMessage.addListener(function(msg){
    //do something with msg contains id of clicked button
  });
}

Fourly, you use jQuery in your script.js, are you injected it as content_script? 第四,您在script.js中使用jQuery,是否将其作为content_script注入? If not, you need to do it by adding jquery to your plugin and injecting it as content_script in manifest: 如果没有,则需要通过向插件中添加jquery并将其作为content_script注入清单中来实现:

  ...
  "content_scripts": [
    {
      "js": [
        "jquery.js"
      ],
      "matches": [
        "http://*/*","https://*/*"
      ]
    }
  ],
  ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM