2014-02-20 30 views
1

我试图让我的页面动作图标显示在特定的网址上。我试过实现here的例子,但是这些需要trunk/dev版本。显示页面特定网址上的动作图标

我现在的代码取自a SO answer。但是这似乎不起作用,因为该选项卡对象从未在我的测试中具有可限制的url属性。

// background.js

function checkURL(tabId, info, tab) { 
    if (info.status === "complete") { 
     if (tab.url) { 
      // restrict here 
      chrome.pageAction.show(tabId); 
     } 
    } 
} 
chrome.tabs.onUpdated.addListener(checkURL); 

//清单

{ 
    "manifest_version": 2, 

    "name": "My first extension", 
    "version": "1.0", 

    "content_scripts": [ 
     { 
      "matches": ["http://www.google.com/*"], 
      "js": [ 
       "script.js" 
      ], 
      "run_at": "document_idle" 
     } 
    ], 

    "background": { 
     "page": "background.html", 
     "persistent": false 
    }, 

    "page_action": { 
     "default_icon": "icon.png" 
    } 
} 

我在做什么错?

回答

2

这个工作对我来说:

//background.js 
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    if (~tab.url.indexOf('.pl')) { 
    chrome.pageAction.show(tabId); 
    } 
}); 

//manifest.json 
"permissions": [ 
"tabs" 
] 

,我没有使用persistent:false

+0

这里是什么的'〜tab.url.indexOf(”的含义。 pl')' –

+0

'〜。“String”.indexOf(“S”)'是判断字符串是否包含子字符串的智能方法。 '〜'为'-1'返回0,即indexOf找不到匹配项。请参阅JS文档中的〜运算符。 –

0

我迟到回答这个问题,但是这可能会帮助其他人有同样的问题。我只花了大约20分钟时间来寻找我自己的扩展。这里https://developer.chrome.com/extensions/declarativeContent

看一下添加到您的清单

"background" : { 
    "scripts": ["background.js"] 
} 
"permissions" : { 
    "declarativeContent" 
} 

然后在background.js

var rule1 = { 
    conditions: [ 
     new chrome.declarativeContent.PageStateMatcher({ 
     // If I wanted my extension to work only on SO I would put 
     // hostContains: 'stackoverflow.com' 
     // You can check out the link above for more options for the rules 
     pageUrl: { hostContains: 'some string' } 
     }) 
    ], 
    actions: [ new chrome.declarativeContent.ShowPageAction() ] 
}; 
chrome.runtime.onInstalled.addListener(function (details) { 
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() { 
     chrome.declarativeContent.onPageChanged.addRules([rule1]) 
    }) 
}) 
相关问题