[英]How to reference popup.html DOM from background.js in chrome extension?
[英]Chrome Extension Saving Values from Background.js into Storage and displaying on popup.html
我目前正在編寫一個簡單的Chrome擴展程序,當您單擊標題中的擴展程序圖標時,會將當前網頁保存到本地存儲。
我已根據Chrome文檔在此處使用background.js將網址成功保存到本地存儲中: https : //developer.chrome.com/extensions/activeTab 。
我的問題是,當我第一次點擊Chrome擴展圖標時,我的事件會觸發但是我的popup.js出錯了
Uncaught TypeError: Cannot read property 'getSelected' of undefined
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
console.log('Turning ' + tab.url + ' red!');
var tabNew = tab.url
chrome.storage.sync.set({ ["key" + 1]: tabNew}, function(){
// A data saved callback omg so fancy
});
chrome.storage.sync.get(/* String or Array */["key" + 1], function(items){
// items = [ { "yourBody": "myBody" } ]
console.log(items)
});
chrome.tabs.executeScript({
code: 'document.body.style.backgroundColor="red"'
});
chrome.browserAction.setPopup({popup: 'popup.html'});
});
popup.js
chrome.tabs.getSelected(null, function(tab) {
document.getElementById('currentLink').innerHTML = tab.url;
});
document.addEventListener('DOMContentLoaded', function() {
var link = document.getElementById('download');
// onClick's logic below:
link.addEventListener('click', function() {
chrome.storage.sync.get(null, function(items) { // null implies all items
// Convert object to a string.
var result = JSON.stringify(items);
// Save as file
chrome.downloads.download({
url: 'data:application/json;base64,' + btoa(result),
filename: 'filename_of_exported_file.json'
});
});
});
});
popup.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
<link href='style.css' rel='stylesheet' type='text/css'>
<title>Discoveroo</title>
<!-- <script type="text/javascript" src="https://www.google.com/jsapi"></script> -->
<base target="_blank">
</head>
<body>
<p id="currentLink">Loading ...</p>
<hr />
<ul id="savedLinks"></ul>
<script type="text/javascript" src="popup.js"></script>
<button id="download">Download All</button>
</body>
</html>
的manifest.json
{
"name": "APPNAME",
"version": "0.1",
"manifest_version": 2,
"description": "APP DESC",
"permissions": ["activeTab", "storage", "downloads", "<all_urls>"],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": ["popup.js"]
}],
"browser_action": {
"default_title": "Add this page to my list"
},
"icons": {
"128": "icon.png"
}
}
<all_urls>
權限,因為您已經擁有activeTab
,在單擊擴展圖標后,您可以完全訪問活動選項卡。 chrome.tabs.getSelected
已棄用,最終將被刪除,因此請使用chrome.tabs.query({active: true, currentWindow: true}, tabs => { ... })
textContent
屬性而不是可能不安全的innerHTML
來顯示純文本字符串,例如URL。 的manifest.json
"background"
"content_scripts"
添加"default_popup"
"browser_action": { "default_title": "Add this page to my list", "default_popup": "popup.html" },
background.js
刪除它。
popup.js
您可能需要的舊后台腳本中唯一的東西是executeScript和sync.set調用。
chrome.tabs.executeScript({
code: 'document.body.style.backgroundColor="red"'
});
chrome.tabs.query({active: true, currentWindow: true}, ([tab]) => {
document.getElementById('currentLink').textContent = tab.url;
chrome.storage.sync.set({key1: tab.url});
});
您還可以加載Mozilla WebExtension polyfill (在popup.js之前使用popup.html中的 <script>
標記)並切換到async / await語法而不是回調:
(async () => {
const [tab] = await browser.tabs.query({active: true, currentWindow: true});
document.getElementById('currentLink').textContent = tab.url;
await chrome.storage.sync.set({key1: tab.url});
})();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.