英文:
No errors show, but Chrome.Storage.Sync. Get is not receiving what I set in Chrome.Storage.Sync.Set
问题
我正在尝试使用扩展的选项部分作为用户编写的网站的一种方式,这些网站稍后将在后台脚本中用于创建这些网站的新标签页。没有错误,但当我尝试运行扩展时,他们放置的网站实际上都没有打开。我不确定这是否与我编写的chrome.storage.sync.set API的方式或我编写的chrome.storage.sync.get API的方式有关,或者完全不同的问题。
以下是我的options.html代码:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<script src="options.js"></script>
<meta charset="utf-8">
<title>Choose Website</title>
</head>
<body>
<h1>Choose Websites Here</h1>
<h4><i>Please Put the full URL</i></h4>
<h4><i>Example: "https://www.google.com"</i></h4>
<h2>Website #1: <input type="text" id="website1"></h2>
<h2>Website #2: <input type="text" id="website2"></h2>
<h2>Website #3: <input type="text" id="website3"></h2>
<button type="button" name="Save Changes" id="savechanges">Save Changes</button>
</body>
</html>
以下是你的options.js代码:
function WebsiteChoosing() {
$('#savechanges').click(function () {
var website1 = $('#website1').val();
var website2 = $('#website2').val();
var website3 = $('#website3').val();
if (website1) {
chrome.storage.sync.set({'website1': website1});
}
if (website2) {
chrome.storage.sync.set({'website2': website2});
}
if (website3) {
chrome.storage.sync.set({'website3': website3});
}
});
}
以下是你的background.js代码(只包括相关部分):
chrome.browserAction.onClicked.addListener(buttonClicked);
function buttonClicked() {
chrome.storage.sync.get(['website1', 'website2', 'website3'], data => {
if (data.website1) chrome.tabs.create({ url: data.website1 });
if (data.website2) chrome.tabs.create({ url: data.website2 });
if (data.website3) chrome.tabs.create({ url: data.website3 });
});
}
以下是你的Manifest.json代码(只包括相关部分):
"background": {
"scripts": ["background.js"]
},
"options_page": "options.html",
"permissions": [
"history",
"storage",
"activeTab",
"tabs"
]
如果你有任何其他问题,欢迎提出。
英文:
I am trying to use the options part of my extension as a way for users to write websites that will later be used in the background script to create new tabs of those websites. There are no errors, but when I try to run the extension none of the websites they put in actually open. I am not sure if this is a problem with the way I coded the chrome.storage.sync.set API or the way I coded the chrome.storag.sync.get API or something completely different.
Here is my options.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<script src="options.js"></script>
<meta charset="utf-8">
<title>Choose Website</title>
</head>
<body>
<h1>Choose Websites Here</h1>
<h4> <i>Please Put the full URL</i></h4>
<h4><i>Example: "https://www.google.com"</i></h4>
<h2>Website #1: <input type="text" id="website1"></h2>
<h2>Website #2: <input type="text" id="website2"></h2>
<h2>Website #3: <input type="text" id="website3"></h2>
<button type="button" name="Save Changes" id="savechanges">Save Changes</button>
</body>
</html>
options.js
function WebsiteChoosing (){
$('#savechanges').click(function(){
var website1 = $('#website1').val();
var website2 = $('#website2').val();
var website3 = $('#website3').val
if(website1){
chrome.storage.sync.set({'website1' : website1})
}
if(website2){
chrome.storage.sync.set({'website1' : website1})
}
if(website3){
chrome.storage.sync.set({'website1' : website1})
}
})
};
backround.js (only relevant)
chrome.browserAction.onClicked.addListener(buttonClicked)
function buttonClicked () {
chrome.storage.sync.get(['website1', 'website2', 'website3'], data => {
if (data.website1) chrome.tabs.create({url: data.website1});
if (data.website2) chrome.tabs.create({url: data.website2});
if (data.website3) chrome.tabs.create({url: data.website3});
});
Manifest.json (only relevant)
"background" : {
"scripts" : ["background.js"]
},
"options_page": "options.html",
"permissions": [
"history",
"storage",
"activeTab",
"tabs"
专注分享java语言的经验与见解,让所有开发者获益!
评论