Display current URL in a chrome extension

前端 未结 3 1314
一个人的身影
一个人的身影 2020-12-09 05:43

After doing some research, the code that I have come up with is this:

var outUrl;
// first get the windowid
chrome.windows.getCurrent(function(window) {
             


        
3条回答
  •  攒了一身酷
    2020-12-09 06:31

    I had the same issue. I wrote this extension to display the current URL user is browsing now in the popup.

    manifest.js

    "permissions": [ 
      "tabs"
    ]
    

    popup.js

    function getCurrentTabUrl(callback) {  
      var queryInfo = {
        active: true, 
        currentWindow: true
      };
    
      chrome.tabs.query(queryInfo, function(tabs) {
        var tab = tabs[0]; 
        var url = tab.url;
        callback(url);
      });
    }
    
    function renderURL(statusText) {
      document.getElementById('status').textContent = statusText;
    }
    
    document.addEventListener('DOMContentLoaded', function() {
      getCurrentTabUrl(function(url) {
        renderURL(url); 
      });
    });
    

提交回复
热议问题