Chrome.Storage.Local.Get主要返回" undefined",但随机开始工作?

时间:2017-10-10 20:22:39

标签: javascript google-chrome google-chrome-extension

我在Chrome扩展程序上存在本地存储问题,可能会让我今晚喝酒。帮我把这个喝成庆祝,而不是沮丧。

我有一个镀铬扩展程序我已编程,其中有一个选项菜单,技术人员可以在其中输入他们的名字(首字母,姓氏)。然后,当扩展程序点击一个按钮以使用提供的信息自动填充模板时,它会提取存储的数据以及所有其他部分,并填写模板。

问题是,它是随机工作的,我无法弄清楚原因。我会让它无处不在,然后将关闭所有内容并重新打开它以查看我是否正在工作以便我可以将其打包并将其部署到其他技术人员...然后返回到undefined 。选项页面似乎正确地存储了这些信息并且每次都返回确切的值,但是使用注入器脚本几乎每次都会失败,当它确实有效时,老实说我不能说出原因。

这就是我现在所拥有的。原谅可能粗略的代码。直到最近才开始研究这些东西。

OPTIONS.HTML

<!DOCTYPE html>
<html>
<head><title>Options</title></head>
<body>
<H1>Call Log Supertool</h1>
</br>
<H3>Technician Name (First Initial, Last Name)</h3>
  <input type="text" id="TechName">
<div id="status"></div>
</br>
<button id="save">Save</button>
<script src="options.js"></script>
</body>
</html>

OPTIONS.JS

    function save_options() {
  var TechName = document.getElementById('TechName').value;
  chrome.storage.local.set({
    TechName: TechName,
  }, function() {
    var status = document.getElementById('status');
    status.textContent = 'Options saved.';
    setTimeout(function() {
      status.textContent = '';
    }, 750);
  });
}


function restore_options() {
  chrome.storage.local.get({
    TechName: 'F.LastName',
  }, function(items) {
    document.getElementById('TechName').value = items.TechName;
  });
}
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click',
    save_options);

INJECTOR.JS

var Textbox = document.getElementById('callnotes');
var AddedNotes = callnotes.value + "Test";
var today = new Date();
var date = (today.getMonth()+1)+'/'+today.getDate()+'/'+today.getFullYear();
var html = document.getElementsByTagName('html')[0];
var text = html.innerHTML;
var TSExtract = text.match(/<strong>TS-....../g);
var TechName;

chrome.storage.local.get(
function(items) {
    var TechName = items.TechName;
  });

if (TSExtract !== null)
    {
var string = TSExtract.toString();
var TSFinal = string.match(/TS-....../g);
}

if (TSExtract==null)
{
document.getElementById("callnotes").value = callnotes.value + "\n \nTS Date: \nLegal Complaint Yes/No: \nComplaint History Review: " + date + " " + TechName + ": A search was conducted and no additional complaints of this nature were found for this patient. \nClinical Study Yes/No:  \nWho told SJM: \nIf shipping/requesting product what is being requested: \nAny other rep/contact names to be added to the record: \nIf product is not returning, document why:"
} else {

document.getElementById("callnotes").value = callnotes.value + "\n \nTS Date:\nLegal Complaint Yes/No: \nComplaint History Review: " + date + " " + TechName + ": A search of the complaint database was performed. Records related to this patient: " + TSFinal + "\nClinical Study Yes/No: \nWho told SJM: \nIf shipping/requesting product what is being requested: \nAny other rep/contact names to be added to the record: \nIf product is not returning, document why:"

}

编辑:不确定这是一个完全重复,虽然最终结果需要相同,我有时会得到我需要的适当的变量响应,它是基于从本地存储拉。虽然在大多数情况下你可以只做一个var foo = document.get命令,特别是chrome.storage.local.get()似乎没有这样做。链接副本通常仅涉及提取变量,这直接影响从本地存储中提取变量。同样,最终的结果,但我不认为这是完全相同的问题......

0 个答案:

没有答案