Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用内容脚本中的jQuery获取DOM对象失败_Javascript_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 使用内容脚本中的jQuery获取DOM对象失败

Javascript 使用内容脚本中的jQuery获取DOM对象失败,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我在谷歌页面上有一个按钮。DOM检查器提供其id。但是尝试获取对象失败。在控制台中编写类名时,我得到了一个未定义的结果。页面中只有一个iframe,而我的对象不在其中(据我所知) manifest.json: { "manifest_version": 2, "name": "modify strings", "description": "modify strings in bulk", "version": "1.0", "permissions":

我在谷歌页面上有一个按钮。DOM检查器提供其id。但是尝试获取对象失败。在控制台中编写类名时,我得到了一个未定义的结果。页面中只有一个iframe,而我的对象不在其中(据我所知)

manifest.json:

{
    "manifest_version": 2,
    "name": "modify strings",
    "description": "modify strings in bulk",
    "version": "1.0",
    "permissions": [
       "tabs",
       "history",
       "http://*/*",
       "https://*/*"
    ],  

    "content_scripts": [
        {
            "matches": ["https://play.google.com/apps/*"],
            "js": ["jquery-1.11.1.min.js", "myInject.js"],
            "all_frames": true
        }
    ],

    "web_accessible_resources": [
        "script.js",
        "jquery-1.11.1.min.js"
    ],

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



}
var buttonJGET = jQuery("gwt-uid-115", document);   
console.log(buttonJGET.className);
myInject.js(内容脚本):

{
    "manifest_version": 2,
    "name": "modify strings",
    "description": "modify strings in bulk",
    "version": "1.0",
    "permissions": [
       "tabs",
       "history",
       "http://*/*",
       "https://*/*"
    ],  

    "content_scripts": [
        {
            "matches": ["https://play.google.com/apps/*"],
            "js": ["jquery-1.11.1.min.js", "myInject.js"],
            "all_frames": true
        }
    ],

    "web_accessible_resources": [
        "script.js",
        "jquery-1.11.1.min.js"
    ],

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



}
var buttonJGET = jQuery("gwt-uid-115", document);   
console.log(buttonJGET.className);
jQuery使用

要使用,您需要使用#。 此外,jQuery总是返回一个结果数组,即使它是唯一的

var buttonJGET = jQuery("#gwt-uid-115")[0];

jQuery元素不同于DOM元素;它们没有
className
属性。 要获得它,可以使用,例如:

console.log(buttonJGET.attr('class'));
还有

否则,您可以:


如果代码仍然失败,可能是因为在脚本运行时,还没有具有该id的元素。要实现“每次添加此类元素时运行我的代码”,可以使用(规范答案):

要使用(注意,筛选器和回调使用DOM元素):

或者,例如,如果要捕获id以
gwt uid
开头的所有节点,可以编写自定义筛选器:

function filter(node) {
  if(node.id && node.id.match(/^gwt-uid/)) return true;
  else return false;
}

var observer2 = watchNodes(filter, logger);
在“document\u start”的
run\u处注入此命令将确保捕获所有添加的元素


若要停止观察,请对返回的观察者对象调用
observer.disconnect()

使用了各种各样的前置器,但仍然无法工作。所以我使用var buttonjet=jQuery(#gwt-uid-115”);但不工作,仍然给我未定义的,我已经正确地重新加载了扩展。更新了我的答案,但请不要编辑掉你的问题:堆栈溢出应该是问题答案的记录。谢谢,我已经编辑回原来的状态;我已经输入了您的更正,但现在我得到一个错误:“UncaughtTypeError:无法读取未定义的属性'className'”这意味着至少在查询执行时没有具有该id的元素(您会同意它看起来是动态的)。你需要重新考虑你的代码是否存在这种可能性。但我想你最初的问题已经得到了回答。我需要在清单中添加什么作为run_at fields,以便脚本在所有可能的时间运行?
function filter(node) {
  if(node.id && node.id.match(/^gwt-uid/)) return true;
  else return false;
}

var observer2 = watchNodes(filter, logger);