Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.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 Chrome扩展错误选项卡.url_Javascript_Google Chrome Extension_Tabs - Fatal编程技术网

Javascript Chrome扩展错误选项卡.url

Javascript Chrome扩展错误选项卡.url,javascript,google-chrome-extension,tabs,Javascript,Google Chrome Extension,Tabs,我基本上只是想获取当前标签的url,如果它们在youtube.com上。我一直从脚本中得到一个错误 错误: 未捕获类型错误:无法调用未定义的方法“getSelected” 显示 { "name": "YouTube Fix", "version": "0.0.1", "manifest_version": 2, "description": &q

我基本上只是想获取当前标签的url,如果它们在youtube.com上。我一直从脚本中得到一个错误

错误:

未捕获类型错误:无法调用未定义的方法“getSelected”

显示

{
    "name": "YouTube Fix",
    "version": "0.0.1",
    "manifest_version": 2,
    "description": "Fix some of the annoying little things in YouTube.",
    "icons": {
        "16": "icon.png",
        "48": "icon.png",
        "128": "icon.png"
    },
    "content_scripts": [{
        "matches": ["http://www.youtube.com/*"],
        "js": ["background.js"],
        "run_at": "document_start"
    }],
    "permissions": ["tabs"]
}
Background.js

//this is what is giving me the error:
chrome.tabs.getSelected(null, function (tab) {
    myFunction(tab.url);
});

function myFunction(tablink) {
    if (tablink == "http://www.youtube.com") {
        window.location = "http://www.youtube.com/feed/subscriptions/u";
    }
    document.getElementById("comments-textarea").disabled = false;
}
引述如下:

chrome.tabs.getSelected()已弃用。下页解释 如何改用chrome.tabs.query: 相关文本位为: """ getAllInWindow()和getSelected()方法已被弃用。 要获取指定窗口中所有选项卡的详细信息,请使用 chrome.tabs.query()和参数{'windowId':windowId}。获取 在指定窗口中选择的选项卡上,使用 参数为{'active':true}的chrome.tabs.query()。 “”“

引述如下:

chrome.tabs.getSelected()已弃用。下页解释 如何改用chrome.tabs.query: 相关文本位为: """ getAllInWindow()和getSelected()方法已被弃用。 要获取指定窗口中所有选项卡的详细信息,请使用 chrome.tabs.query()和参数{'windowId':windowId}。获取 在指定窗口中选择的选项卡上,使用 参数为{'active':true}的chrome.tabs.query()。 “”“


chrome.tabs.getSelected
is。改用:

此外,内容脚本
chrome.tabs
API。这样做:

chrome.extension.getBackgroundPage().chrome.tabs.query(...

(这可能不起作用。未测试。)

chrome.tabs.getSelected
正在运行。改用:

此外,内容脚本
chrome.tabs
API。这样做:

chrome.extension.getBackgroundPage().chrome.tabs.query(...

(这可能不起作用。未测试。)

您正在将background.js作为内容脚本而不是后台或事件页面运行,并说“内容脚本有一些限制。它们不能…使用chrome.*API(chrome.extension的部分除外)”

相反,你应该把

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

进入你的舱单。有关更多详细信息,请参阅。

您正在将background.js作为内容脚本而不是后台或事件页面运行,并表示“内容脚本有一些限制。它们不能…使用chrome.*API(chrome.extension的部分除外)”

相反,你应该把

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

进入你的舱单。有关更多详细信息,请参阅。

以下分叉很好,不需要
chrome.tabs.getSelected(null,function(tab){})
,因为您的页面总是运行
“匹配项”:http://www.youtube.com/*“],

如果(document.getElementById(“comments textarea”)!=null){

工作背景.js

 if (window.location == "http://www.youtube.com") {
        window.location = "http://www.youtube.com/feed/subscriptions/u";
    }
    if (document.getElementById("comments-textarea") != null) {
        document.getElementById("comments-textarea").disabled = false;
    }
manifest.json

{
    "name": "YouTube Fix",
    "version": "0.0.1",
    "manifest_version": 2,
    "description": "Fix some of the annoying little things in YouTube.",

    "content_scripts": [{
        "matches": ["http://www.youtube.com/*"],
        "js": ["background.js"],
        "run_at": "document_start"
    }],
    "permissions": ["tabs"]
}

如果您需要更多信息,请告诉我。

以下分叉很好,不需要
chrome.tabs.getSelected(null,function(tab){})
,因为您的页面总是运行
“匹配项”:http://www.youtube.com/*“],

如果(document.getElementById(“comments textarea”)!=null){

工作背景.js

 if (window.location == "http://www.youtube.com") {
        window.location = "http://www.youtube.com/feed/subscriptions/u";
    }
    if (document.getElementById("comments-textarea") != null) {
        document.getElementById("comments-textarea").disabled = false;
    }
manifest.json

{
    "name": "YouTube Fix",
    "version": "0.0.1",
    "manifest_version": 2,
    "description": "Fix some of the annoying little things in YouTube.",

    "content_scripts": [{
        "matches": ["http://www.youtube.com/*"],
        "js": ["background.js"],
        "run_at": "document_start"
    }],
    "permissions": ["tabs"]
}

如果您需要更多信息,请告诉我。

被弃用并不意味着它已被删除。“chrome.tabs.getSelected”在测试背景页面上运行良好。此外,如果问题是getSelected已被删除,则错误将是“Uncaught TypeError:Object”没有方法“getSelected”@JeffreyYasskin-对不起,但是在回答中,哪里说它不再有效了?说getSelected已被弃用与问题无关,因为修复该问题不会让作者更接近于拥有一个可用的扩展。这当然是正确的,值得评论,但这不是一个答案。弃用并不意味着它无效删除。“chrome.tabs.getSelected”在测试背景页面上运行正常。此外,如果问题是getSelected已删除,则错误将是“Uncaught TypeError:Object”没有方法“getSelected”@JeffreyYasskin-对不起,但是在回答中,哪里说它不再工作了?说getSelected已被弃用与问题无关,因为修复该问题不会让作者更接近于拥有一个工作扩展。这当然是正确的,值得评论,但这不是一个答案。这可能更接近于有用韩:我的答案。注意原始代码在
http://www.youtube.com/*
但测试准确
http://www.youtube.com/
(即主页),因此他们可能仍然需要第一个
if
@JeffreyYasskin:谢谢你指出它,更新了我的解决方案。这可能比我的答案更有用。请注意,原始代码运行于
http://www.youtube.com/*
但测试准确
http://www.youtube.com/
(即主页),所以他们可能仍然需要第一个
if
@JeffreyYasskin:谢谢你指出它,更新了我的解决方案。