Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
Google chrome extension 从脚本访问数据层_Google Chrome Extension - Fatal编程技术网

Google chrome extension 从脚本访问数据层

Google chrome extension 从脚本访问数据层,google-chrome-extension,Google Chrome Extension,我正在开发一个根据规则验证页面的扩展,例如,我正在检查数据层中的值是否与我在各种元素中期望的值相对应。我的测试代码在粘贴到控制台时可以完美地工作。我的目标是将此转移到扩展 我从弹出窗口调用内容脚本中的函数,内容脚本如下 function testcode (avalue){ alert(avalue) //This works data_element_type_count = $('a[data-element-type]').length + 1; alert

我正在开发一个根据规则验证页面的扩展,例如,我正在检查数据层中的值是否与我在各种元素中期望的值相对应。我的测试代码在粘贴到控制台时可以完美地工作。我的目标是将此转移到扩展

我从弹出窗口调用内容脚本中的函数,内容脚本如下

function testcode (avalue){
    alert(avalue)
    //This works
    data_element_type_count = $('a[data-element-type]').length + 1;
    alert(data_element_type_count)

    //This doesn't
    productListing_count = hcDatalayer.productListing.length + 1;
    alert(productListing_count)
}


testcode ('hello')
我得到:uncaughtreferenceerror:hcDatalayer未定义

当页面加载时,如果我从弹出窗口运行它,同样的位会失败

Error in event handler for runtime.onMessage: ReferenceError: hcDatalayer is not defined
    at testcode (chrome-extension://lbcndjkkmngpdcgojifgolgchhaciipb/contentscript.js:49:28)
    at chrome-extension://lbcndjkkmngpdcgojifgolgchhaciipb/contentscript.js:8:4
如果在控制台中粘贴以下内容,则可以正常工作

productListing_count = hcDatalayer.productListing.length + 1;
alert(productListing_count)
我可以想象它在页面加载时不起作用,因为可能还没有推送值,但从弹出窗口中我不确定它为什么不起作用,它显然可以访问页面,因为这一点起作用

data_element_type_count = $('a[data-element-type]').length + 1;
alert(data_element_type_count)
在控制台上,它可以工作。我错过了什么?任何帮助都将不胜感激

谢谢


Grant

从弹出页面调用内容脚本函数,您应该在此处使用消息传递API:从弹出页面调用内容脚本函数,您应该在此处使用消息传递API:从弹出页面调用内容脚本函数,您应该在此处使用消息传递API: