将选项保存到Chrome存储失败

时间:2018-10-31 18:18:50

标签: javascript html google-chrome-extension

我无法将选项保存到Chrome存储设备中。控制台中没有错误,但只是不保存选项。感谢@wOxxOm帮助解决原始错误。

如果您想要完整的代码-大部分在下面。

有任何解决建议吗?

下面的完整代码段。

// Saves options to chrome.storage
function save_options() {
  var logolink = document.getElementById('logourl');
  var company = document.getElementById('companyname');
  var gst = document.getElementById('gstnumber');
  var footert = document.getElementById('footertext');
  chrome.storage.sync.set({
    logoStore: logolink,
	companyStore: company,
	gstStore: gst,
	footerStore: footert
  }, function() {
    // Update status to let user know options were saved.
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
  });
}

// Restores select box and checkbox state using the preferences
// stored in chrome.storage.
function restore_options() {
  // Use default value logoStore = 'original url' plus others.
  chrome.storage.sync.get({
    logoStore: 'your url',
 	companyStore: 'Your Company Name',
	gstStore: '123-123-123',
	footerStore: 'Footer'
  }, function(items) {
    document.getElementById('logourl').textContent = items.logoStore;
    document.getElementById('companyname').textContent = items.companyStore;
    document.getElementById('gstnumber').textContent = items.gstStore;
    document.getElementById('footertext').textContent = items.footerStore;
  });
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);

此处的HTML。

<!doctype html>
<html>
  <head>
    <title>Invoice Personaliser - Options</title>
    <link rel="stylesheet" type="text/css" href="options.css">
  </head>
  <body>
  <div class=container>
<div class="main">
  <h1>Invoice Personaliser - Options</h1>
   <form id="form">
<h3>Replacement Logo URL</h3>
   <input type="text" id="logourl">
<h3>Company Name</h3>
 <input type="text" id="companyname">
<h3>GST Number</h3>
 <input type="text" id="gstnumber">
<h3>Footer Text</h3>
 <input type="text" id="footertext">
    <button id="save">Save Options</button>
	</form>
   <div id="status"></div>

   </div>
   </div>
       <script src="options.js"></script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

在每个getElementById('')之后使用.value-现在可以代替.textContent使用。