简体   繁体   English

Chrome扩展程序:尝试获取选项卡URL时,Background.js“未捕获的TypeError”

[英]Chrome Extension: Background.js “Uncaught TypeError” when trying to get tab url

I'm getting following warning/error from my extension when opening debugging tool (all the following code works perfect, but it is throwing this error): 打开调试工具时,我从扩展程序中收到警告/错误(以下所有代码均正常运行,但会引发此错误):

Uncaught TypeError: Cannot read property 'onUpdated' of undefined 未捕获的TypeError:无法读取未定义的属性“ onUpdated”

In my code below I'm trying to get the current tab url for the browser. 在下面的代码中,我试图获取浏览器的当前选项卡URL。 (This is a background.js script) (这是一个background.js脚本)

chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab) {
    chrome.tabs.getSelected(null,function(tab) {
        var tablink = tab.url;
        console.log("Website: " + tablink);
    });
});

This script above is my background script, called from manifest. 上面的脚本是我的后台脚本,从清单中调用。

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

My question is now, why does it throw an error when the code works? 我的问题是,为什么在代码正常工作时会引发错误? Error is displayed in the chrome "debugging / inspect-element". 错误显示在镶边的“调试/检查元素”中。 How can I remove this so called "error" from the debugging? 如何从调试中删除所谓的“错误”?

EDIT After using a "whoami" call from content script to background script i get following error on background script. 编辑使用从内容脚本到后台脚本的“ whoami”调用后,我在后台脚本上收到以下错误。

providing tab information background.js:3 Error in event handler for runtime.onMessage: Cannot read property 'url' of undefined Stack trace: TypeError: Cannot read property 'url' of undefined at chrome-extension://hejlnkjmnpomhknnbfjdfnablcmkloid/background.js:4:41 at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14) at EventImpl.dispatchToListener (extensions::event_bindings:397:22) at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14) at Event.publicClass.(anonymous function) [as dispatchToListener] (extensions::utils:89:26) at EventImpl.dispatch_ (extensions::event_bindings:379:35) at EventImpl.dispatch (extensions::event_bindings:403:17) at Function.target.(anonymous function) (extensions::SafeBuiltins:19:14) at Event.publicClass.(anonymous function) [as dispatch] (extensions::utils:89:26) at messageListener (extensions::messaging:190:29) extensions::uncaught_exception_handler:9

There's some minor defects in your manifest file. 清单文件中有一些小缺陷。 The script files you use in your popup or options page should not be declared as content scripts. 您在弹出窗口或选项页面中使用的脚本文件不应声明为内容脚本。 Including them into their respective html files is enough (which you are already doing), considering you're not planning to inject them into other external web pages. 考虑到您不打算将它们注入其他外部网页,将它们包含在各自的html文件中就足够了(您已经在做)。

Popup and option pages are not content scripts . 弹出页面和选项页面不是内容脚本 They are privileged to use the Chrome APIs just as background scripts are. 他们有特权像后台脚本一样使用Chrome API。

Content scripts are injected into other web sites. 内容脚本被注入其他网站。 If you are really meaning to do that, you should most likely also include the "run_at" property. 如果您确实有意这样做,则很可能还应包括“ run_at”属性。 Then again, it depends on what you want to do with it. 再说一次,这取决于您要使用它做什么。

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

However, content scripts don't have the privilege to access the tabs API. 但是,内容脚本没有访问选项卡API的特权。 Using your background scripts simultaneously as content scripts leads to those type errors and might eventually break your extension, yet it still might seem as though your extension is working - in the scope of the background scripts (popup, options), at least. 同时使用后台脚本作为内容脚本会导致这些类型错误,并可能最终破坏您的扩展名,但似乎扩展名仍在起作用-至少在后台脚本的范围内(弹出窗口,选项)。

Strictly separate background and content scripts. 严格分开的背景和内容脚本。 If you don't need to inject code into other web sites, I'd suggest you remove it alongside with the permissions - the permission warning when installing your extension would be unnecessarily demanding if you ask for permission to read and modify user data on all web sites , anyway. 如果您不需要将代码注入其他网站,建议您将其与权限一起删除-如果您要求扩展权限来读取和修改所有用户数据,则在安装扩展程序时的权限警告会不必要网站 ,无论如何。

I don't directly know why your code doesn't work, but : 我不直接知道为什么您的代码不起作用,但是:

First, getSelected is now deprecated, please refer to the doc https://developer.chrome.com/extensions/tabs#method-getSelected and use tabs.query {active: true} ;) 首先,现已弃用getSelected,请参阅doc https://developer.chrome.com/extensions/tabs#method-getSelected并使用tabs.query {active:true};)

Second, here there's a kind of "ambiguity" on which "tab" variable is used when you write "tab.url" is it the one from getSelected, or from Updated event ? 其次,这里有一种“歧义性”,当您编写“ tab.url”时,将使用“ tab”变量,它是getSelected还是Updated事件中的变量? (if it's the last, getSelected is useless) (如果是最后一个,则getSelected是无用的)

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

相关问题 我的“background.js”代码用于获取选定的 chrome 标签 url 在我的 chrome 扩展中无法正常工作 - My “background.js” code to get the selected chrome tabs url is not working properly in my chrome extension Chrome 扩展 - 当它不是活动标签时,background.js 可以访问本地 html 页面吗? - Chrome Extension - Can background.js access a local html page when it's not the active tab? 从Chrome扩展程序的background.js获取响应 - Get Response From background.js of chrome extension 在 background.js 中的 Chrome 扩展中的 setInterval - setInterval in Chrome extension in background.js chrome 扩展中 background.js 的用途是什么? - What is the purpose of background.js on a chrome extension? Background.js无法使用Chrome扩展程序 - Background.js not working Chrome Extension Background.js中的Chrome扩展Keydown侦听器 - Chrome extension keydown listener in background.js Chrome扩展程序在Background.js中注册事件 - Chrome Extension Registering Events in Background.js 我正在尝试让jQuery在我的chrome扩展程序的background.js中工作,并且它将无法工作。 为什么? - I'm trying to get jquery working in the background.js of my chrome extension and it won't work. why? 从background.js到content.js的chrome扩展消息,带有响应,TypeError无法读取属性 - chrome extension message from background.js to content.js with response, TypeError cannot read property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM