天天看点

popup.js获取页面网址和标题

//popup.js

chrome.tabs.query({active: true,currentWindow: true},function(tabs) {
        
        var url = tabs[0].url;
        var title = tabs[0].title;
       alert(title + url);
       })
           
//popup.js
chrome.windows.getCurrent(function (currentWindow) {
 chrome.tabs.query({active: true, windowId: currentWindow.id}, function (tabs) {
  var title=tabs[0].title;
    title=title.split("-")[0];
      title=title.replace(/^\s+|\s+$/g, ''); // 去除头尾空格  
   //alert("当前tabs页面地址url + 标题title:" + title + " " + tabs[0].url);
  
    
   });
 });
           
//popup.js
chrome.tabs.getSelected(null, function (tab) { // 先获取当前页面的tabID
     
alert(tab.title);
 
alert(tab.url);
});
           

继续阅读