簡體   English   中英

如何在javascript(chrome擴展名)中獲取選項卡或窗口的當前URL

[英]How to get the current URL of a tab or a window in javascript(chrome extension)

我正在使用一個Chrome擴展程序,可以在其中共享當前處於活動狀態的標簽頁或窗口的URL。 我在獲取javascript中當前處於活動狀態的URL時遇到了一些麻煩。

試着

var current_url = window.location.host;
, var current_url = window.location.hostname;
, var current_url = window.location.href;
and var current_url = document.location.href;

這是URL傳遞函數的代碼:(background.js):

 chrome.runtime.onMessage.addListener(function(message,sender,sendResponse){
      if(message.message=='openurl'){

                chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
                  var tab = tabs[0];
                  var page_url = tab.url;
                  wayback_url = message.wayback_url;
                  var pattern = /https:\/\/web\.archive\.org\/web\/(.+?)\//g;
                  url = page_url.replace(pattern, "");
                  open_url = wayback_url+encodeURI(url);

                  if(message.method!='save'){
                    status = wmAvailabilityCheck(page_url,
                                        function(){ chrome.tabs.create({ url:  open_url});
                                            sendResponse({status:true});
                                          },
                                        function(){
                                            //alert("URL not found in wayback archives!");
                                            sendResponse({status:false});
                                        });
                 }
                else{
                    chrome.tabs.create({ url:  open_url});
                  }

              });
      }
      else if(message.message=='geturl') {
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
              var tab = tabs[0];
              var page_url = tab.url;
              if(isValidSnapshotUrl(page_url)){
                sendResponse({url: page_url});
              }
          });
      }
       return true; 
    });

popup.js的代碼:

  function get_alexa_info(){
    chrome.runtime.sendMessage({message: "geturl" }, function(response) {
     shareon_facebook(response.url);
    shareon_twitter(response.url);
    shareon_googleplus(response.url);
  }

function shareon_facebook(url)
{
    var current_url = url;
    var fbshr_url = "https://www.facebook.com/sharer/sharer.php?u="
    window.open(fbshr_url + current_url , 'newwindow', 'width=500, height=400');
}

function shareon_twitter(current_url)
{

    var twitshr_url = "https://twitter.com/home?status=";
    window.open(twitshr_url + current_url , 'newwindow', 'width=500, height=400');
}

function shareon_googleplus(url)
{

    var gplusshr_url = "https://plus.google.com/share?url="; 
    window.open(gplusshr_url + url , 'newwindow', 'width=500, height=400');
}

由於其他原因,我無法提供有關此擴展程序的更多信息。

如果要獲取活動選項卡的URL,請嘗試以下操作:

chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function 
(tabs) {
    var url = tabs[0].url;
    document.getElementById("host").innerHTML = url;
});

然后,您可以將其顯示在html文件中,如下所示:

<div id="host"></div>

暫無
暫無

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

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