2010-11-16 157 views
0

我正在制作一个Chrome扩展程序来操作当前的选项卡。 当我给它的具体网址,它的作品。谷歌浏览器扩展程序操作网址

<html> 
    <script> 

function updateUrl(tab){ 

     var newurl = "http:www.google.com"; 
     chrome.tabs.update(tab.id, {url: newurl}); 
} 

chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);}); 

    </script> 
</html> 

但是,当我将其更改为以下时,它不起作用。

<html> 
    <script> 

var currentURl = tab.url 

function updateUrl(tab){ 

     var newurl = currentURL.replace("bing", "google"); 
     chrome.tabs.update(tab.id, {url: newurl}); 
} 

chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);}); 

    </script> 
</html> 

我试图改变“tab.url”的东西,如“chrome.tab.url”“chrome.tabs.url”和其他一些人。 有什么想法? 感谢先进! :)

回答

0

标签在分配未定义

var currentURL = tab.url 

您必须将监听器updateURL中定义CURRENTURL

尝试

<html> 
    <script> 

    function updateUrl(tab){ 

     var currentURL = tab.url 

     var newurl = currentURL.replace("bing", "google"); 
     chrome.tabs.update(tab.id, {url: newurl}); 
    } 

    chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);}); 

    </script> 
</html>