簡體   English   中英

Google Chrome擴展程序操作網址

[英]Google Chrome Extension Manipulate URL

我正在制作可操作當前標簽頁的Chrome擴展程序。 當我給它特定的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>

但是,當我將其更改為以下內容時,它將不起作用。

<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”和其他一些內容。 有任何想法嗎?! 提前致謝! :)

選項卡在分配中未定義

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM