简体   繁体   English

Google Chrome扩展程序操作网址

[英]Google Chrome Extension Manipulate URL

I'm making a Chrome Extension that manipulates the current tabs. 我正在制作可操作当前标签页的Chrome扩展程序。 When I give it the specific URL, it works. 当我给它特定的URL时,它可以工作。

<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.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);});

  </script>
</html>

I've tried changing the "tab.url" to things such as "chrome.tab.url" "chrome.tabs.url" and a number of others. 我尝试将“ tab.url”更改为诸如“ chrome.tab.url”,“ chrome.tabs.url”和其他一些内容。 Any ideas?! 有任何想法吗?! Thanks in advanced! 提前致谢! :) :)

tab is undefined in the assignment 选项卡在分配中未定义

var currentURL = tab.url

You must define currentURL within your function updateUrl 您必须在函数updateUrl中定义currentURL

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM