2012-12-09 84 views
0

我基本上只是试图获取当前标签网址,如果他们在youtube.com上。我不断收到脚本中的错误。Chrome扩展错误tab.url

Error:

Uncaught TypeError: Cannot call method 'getSelected' of undefined

清单

{ 
    "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; 
} 

回答

0

从这里引:https://groups.google.com/a/chromium.org/forum/?fromgroups=#!topic/chromium-extensions/A5bMuwCfBkQ

chrome.tabs.getSelected() is deprecated. The following page explains how to use chrome.tabs.query instead: http://code.google.com/chrome/extensions/whats_new.html#16 The relevant bit of text is: """ The methods getAllInWindow() and getSelected() have been deprecated. To get details about all tabs in the specified window, use chrome.tabs.query() with the argument {'windowId': windowID}. To get the tab that is selected in the specified window, use chrome.tabs.query() with the argument {'active': true}. """

0

chrome.tabs.getSelecteddeprecated。使用chrome.tabs.query代替:

chrome.tabs.query({ 
    "active": true 
}, function(tab){ 
    console.log(tab[0]); //selected tab 
}); 

另外,内容脚本can not accesschrome.tabs的API。这样做:

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

(。这可能无法正常工作未测试)

+0

被弃用并不意味着它被删除。 “chrome.tabs.getSelected”在测试后台页面中工作得很好。此外,如果问题是getSelected已被删除,错误将会是“Uncaught TypeError:Object# has no method'getSelected'”。 –

+0

@JeffreyYasskin - 我很抱歉,但答案中的哪里说它不再工作? –

+0

说getSelected已被弃用与解决这个问题无关,这个问题不会让作者更接近于工作扩展。评论当然是真实有用的,但这不是一个答案。 –

1

您正在运行background.js为内容脚本,而不是一个背景或活动页面,并https://developer.chrome.com/extensions/content_scripts.html说:“内容脚本有一定的局限性,他们不能...使用Chrome。*的API(除了chrome.extension部分)”

相反,你应该把类似

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

我你的清单。有关更多详细信息,请参阅https://developer.chrome.com/extensions/event_pages.html

2

以下分叉罚款,不需要chrome.tabs.getSelected(null, function (tab) {}),因为您的网页总是运行"matches": ["http://www.youtube.com/*"],;

更多在这种情况下添加到您的代码if(document.getElementById("comments-textarea") != null){

工作background.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"] 
} 

让我知道如果你需要了解更多信息。

+1

这可能比我的答案更有用。请注意,原始代码在'http:// www.youtube.com/*'上运行,但是完全测试了http:// www.youtube.com /'(即主页),因此它们可能仍然需要第一个'if'。 –

+0

@JeffreyYasskin:感谢您指点,更新了我的解决方案。 – Sudarshan