2015-06-14 194 views
0

我的Chrome擴展程序會加載本地.html文件。JSON - 獲取Chrome標籤標題

(內manifest.json檔案)

"default_popup": "popup.html", 

內部的HTML文件,幀創建www.website.com,因此能夠查看www.website.com在小彈出我擴展名創建(使用popup.js)。

<iframe src="http://www.website.com" width="100%" height="100%" frameborder="0"> 

我想爲我的Chrome擴展程序改變「www.website.com」添加到它的當前選項卡中的Chrome用戶正在瀏覽的標題末尾,減去任何空間和減少長度最多30個字符。

例如: Bob已將Chrome打開到www.pbs.org,其標題爲「PBS公共廣播服務」。他點擊我的Chrome擴展程序並下載,在下拉窗口中向他顯示​​www.website.com/PBSPublicBroadcastingService。鮑勃很高興。

這裏是什麼樣的彈出式看起來像一個截圖: http://julianapena.com/wp-content/uploads/HowtobuildaChromeextensionPart3Loadingan_13EA3/image_thumb.png

回答

0

按照https://developer.chrome.com/extensions/tabs

可以 添加「權限」: 「標籤」 ],在您的清單文件,在helper.js文件中添加

function getCurrentTab(callback) { 
    chrome.tabs.query({ active: true, currentWindow: true }, function (tab) { 
    tab = tab[0]; 
    return callback(tab); 
    }); 
} 

,並添加您的popup.js

function Init() { 
    getCurrentTab(function(tab) { 
    ShowIframe(tab.title); 
    }); 
} 

document.addEventListener('DOMContentLoaded', Init); 

您還可以創建chromeUI.js,您可以在其中定義ShowIframe函數。

+0

我將您提供的代碼的第一位添加到我的manifest.json文件中,但它返回一個錯誤。下面是代碼我有: –

+0

「權限」:[ 「標籤」, 功能getCurrentTab(回調){ chrome.tabs.query({活性:真,currentWindow:真},函數(製表符){​​ 標籤=標籤[0]; 返回回調(標籤); –

+0

「權限」:[ 「標籤」 ]在清單文件中,請看https://developer.chrome.com/extensions/tabs,然後您可以創建在你的擴展中使用helper.js文件或者在彈出窗口js中定義getCurrentTab函數 –