0

I'm trying to develop a google chrome extension which can block specific URL. For this with the help of google I've made following code, but the code is not work perfectly.

The code :

manifest.json

{
  "manifest_version": 2,

  "name": "Zitu-LinkBlocker",
  "description": "A link blocker extension!",
   "permissions": [  
      "tabs"  
     ],  
  "version": "1.0",
  "icons": {
    "128": "icon128.png",
    "48": "icon48.png",
    "16": "icon16.png"
  },

  "browser_action": {
    "default_icon": "icon16.png",
    "default_popup": "popup.html"
  }
}

popup.js

 $(document).ready(function() {   
   chrome.tabs.query(null, function (tab) {  
     var link = document.createElement('a');  
     link.href = tab.url;  
     $('#host').html("Host : "+link.hostname);  
   })  
 });  

popup.html

<!doctype html>  
 <html>  
  <head>  
   <title>Getting Started Extension's Popup</title>  
   <style>  
    body {  
     min-width: 357px;  
     overflow-x: hidden;  
    }  
   </style>  
    <script src="jquery-3.5.1.min.js"></script>  
    <script src="popup.js"></script>     
  </head>  
  <body> 
Yo Man  
    <div id="host"></div>  
  </body>   
 </html>  
Maruf Hossain
  • 101
  • 1
  • 3
  • 17

1 Answers1

1

I think you need to pass { active: true, currentWindow: true } instead of null in chrome.tabs.query method. The tab parameter inside the callback will be an array and you need to access the URL using tab[0].url.

manifest.json

{
    "manifest_version": 2,
    "name": "Zitu-LinkBlocker",
    "description": "A link blocker extension!",
     "permissions": [  
        "tabs"  
       ],  
    "version": "1.0",
    "browser_action": {
      "default_popup": "popup.html"
    }
  }

popup.html

<!doctype html>
<html>
<head>
    <title>Getting Started Extension's Popup</title>
    <style>
        body {
            min-width: 357px;
            overflow-x: hidden;
        }
    </style>
    <script src="jquery.min.js"></script>
    <script src="popup.js"></script>
</head>
<body>
    Yo Man
    <div id="host"></div>
</body>
</html>

popup.js

$(document).ready(function () {
    chrome.tabs.query({ active: true, currentWindow: true }, (tab) => {
        console.log(tab)
        var link = document.createElement('a');
        link.href = tab[0].url;
        $('#host').html("Host : " + link.hostname);
    });
});  
Noob dev
  • 134
  • 6