开发者

Google Chrome Extension Manipulate URL

I'm making a Chrome Extension that manipulates the current tabs. When I give it the specific URL, it works.

<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>

However, when I change it to the following, it doesn't work.

<html>
  <script>

var currentURl = tab.url

function updateUrl(tab){

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

chrome.brow开发者_JAVA百科serAction.onClicked.addListener(function(tab) {updateUrl(tab);});

  </script>
</html>

I have tried changing the "tab.url" to things such as "chrome.tab.url" "chrome.tabs.url" and a number of others.


tab is undefined in the assignment

var currentURL = tab.url

You must define currentURL within your function updateUrl.

Try:

<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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜