本文介绍了Google Chrome扩展chrome.tabs.onUpdated.addListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:未捕获TypeError:无法读取未定义的属性onUpdated



Google Chrome扩展程序



我的代码:

main.js



我有一个函数getCookie和setcookie

  var _a = getCookie(a); 

if(_a!=){
///做某事
} else {


chrome.tabs.onUpdated .addListener(function(tabId,info,tab){
if(info.status ==complete){

var _a = document.getElementsByName('id_loaded_pa​​ge')[0] .value;
setCookie(_ a,value,1);
console.log(_ a:+ _a);



});



解决方案

你是从内容脚本中调用 chrome.tabs



通过,内容脚本为到大多数Chrome API。



您需要创建一个后台页面来访问 chrome.tabs

code>,但在你的特殊情况下,你甚至不需要这个包装器:你在注入document_end,这意味着所有的静态DOM是已经加载了

如果您正在查找的DOM节点是动态添加的,则当完成触发选项卡时,它可能不存在。您需要听取。


Problem: Uncaught TypeError: Cannot read property 'onUpdated' of undefined

Google Chrome extension

My code:

main.js

I have a function getCookie and setcookie

var _a = getCookie("a");

if (_a != "") {
/// do something
} else {


chrome.tabs.onUpdated.addListener(function(tabId , info , tab) {
if (info.status == "complete") {

   var _a = document.getElementsByName('id_loaded_page')[0].value;
       setCookie("_a", value, 1);
       console.log("_a: " +_a);


   }
});

}
解决方案

You are calling chrome.tabs from a Content Script.

By design, content scripts are not allowed access to most of the Chrome APIs.

You need to make a background page to access chrome.tabs, but in your particular case you don't even need that wrapper: you're injecting at "document_end", which should mean that all static DOM is already loaded

And if the DOM node you are looking for is dynamically added, it may not exist when "complete" fires for a tab. You will need to listen to DOM mutations.

这篇关于Google Chrome扩展chrome.tabs.onUpdated.addListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:27