简体   繁体   English

所有chrome.tabs都出现相同的错误

[英]All chrome.tabs present the same error

I'm trying to make an extension that reloads a tab every 2 minutes, but I want it to try and reload even if the internet was offline (so it would reload when it went back on). 我正在尝试制作一个每2分钟重新加载一个标签的扩展程序,但我希望它尝试重新加载即使互联网处于脱机状态(因此它会在重新启动时重新加载)。 Using location.reload() doesn't work when the window gives an error (eg offline), so I figured the best way was using chrome.tabs.reload(). 当窗口发出错误(例如离线)时,使用location.reload()不起作用,所以我认为最好的方法是使用chrome.tabs.reload()。

The problem is, all chrome.tabs give me a similar error, if I try with an empty argument it should work, since it defaults to the current tab according to the documentation, but instead: 问题是,所有chrome.tabs给我一个类似的错误,如果我尝试使用一个空参数它应该工作,因为它根据文档默认为当前选项卡,而是:

chrome.tabs.reload({});
Uncaught TypeError: Cannot call method 'reload' of undefined 

If I try to get the current tab ID: 如果我尝试获取当前标签ID:

chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
  console.log(tabs[0]);
});
Uncaught TypeError: Cannot call method 'query' of undefined 

And similarly, every single chrome.tabs has a similar error, "Cannot call xxyyzz of undefined". 同样,每个chrome.tabs都有类似的错误,“不能调用未定义的xxyyzz”。 It's as if chrome couldn't see my tabs, what's going on? 这就好像铬无法看到我的标签,发生了什么?

My manifest.js is: 我的manifest.js是:

{
  "manifest_version": 2,

  "name": "test",
  "description": "",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "js": ["reload.js"],
      "run_at": "document_end"

    }
  ],
  "permissions": [
    "tabs","storage","http://www.google.com"
  ]
}

You cannot access chrome.tabs from inside a content script, 无法从内容脚本中访问 chrome.tabs

[...] content scripts have some limitations. [...]内容脚本有一些限制。 They cannot: 他们不可以:

  • Use chrome.* APIs (except for parts of chrome.extension) eg chrome.tabs 使用chrome。* API(chrome.extension的部分除外), 例如chrome.tabs
  • Use variables or functions defined by their extension's pages 使用其扩展页面定义的变量或函数
  • Use variables or functions defined by web pages or by other content scripts 使用由网页或其他内容脚本定义的变量或函数

[...] [...]

You have to use a Background Page to use the chrome.tabs Api Methods . 您必须使用Background Page才能使用chrome.tabs Api方法

And invoke reload from there. 并从那里调用reload

Update: 更新:
I added a simple sample background.js which toggles Autoreloading for a tab (2 Minutes) through a contextmenu click. 我添加了一个简单的示例background.js ,它通过上下文菜单点击切换选项卡的自动加载(2分钟)。 I tried to comment it as well as i can 我尽力评论它

Spoiler Warning: Do not click this link if you do this as a learning practice (or anything similar) for yourself otherwise feel free to do whatever you want with the code 扰流警告: 不要点击这个链接 ,如果你做到这一点作为学习实践(或任何类似),为自己,否则随意做任何你想要的代码

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

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