No errors show, but Chrome.Storage.Sync. Get is not receiving what I set in Chrome.Storage.Sync.Set

huangapple 未分类评论43阅读模式
英文:

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
  &lt;head&gt;
    &lt;script src=&quot;options.js&quot;&gt;&lt;/script&gt;
    &lt;meta charset=&quot;utf-8&quot;&gt;
    &lt;title&gt;Choose Website&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Choose Websites Here&lt;/h1&gt;
    &lt;h4&gt; &lt;i&gt;Please Put the full URL&lt;/i&gt;&lt;/h4&gt;
    &lt;h4&gt;&lt;i&gt;Example: &quot;https://www.google.com&quot;&lt;/i&gt;&lt;/h4&gt;
    &lt;h2&gt;Website #1: &lt;input type=&quot;text&quot; id=&quot;website1&quot;&gt;&lt;/h2&gt;
    &lt;h2&gt;Website #2: &lt;input type=&quot;text&quot; id=&quot;website2&quot;&gt;&lt;/h2&gt;
    &lt;h2&gt;Website #3: &lt;input type=&quot;text&quot; id=&quot;website3&quot;&gt;&lt;/h2&gt;
    &lt;button type=&quot;button&quot; name=&quot;Save Changes&quot; id=&quot;savechanges&quot;&gt;Save Changes&lt;/button&gt;
  &lt;/body&gt;
&lt;/html&gt;

options.js

function WebsiteChoosing (){
  $(&#39;#savechanges&#39;).click(function(){
    var website1 = $(&#39;#website1&#39;).val();
    var website2 = $(&#39;#website2&#39;).val();
    var website3 = $(&#39;#website3&#39;).val
    if(website1){
      chrome.storage.sync.set({&#39;website1&#39; : website1})
    }
    if(website2){
      chrome.storage.sync.set({&#39;website1&#39; : website1})
    }
    if(website3){
      chrome.storage.sync.set({&#39;website1&#39; : website1})
  }
})

  };

backround.js (only relevant)

chrome.browserAction.onClicked.addListener(buttonClicked)

function buttonClicked () {

chrome.storage.sync.get([&#39;website1&#39;, &#39;website2&#39;, &#39;website3&#39;], data =&gt; {
  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)

 &quot;background&quot; : {
      &quot;scripts&quot; : [&quot;background.js&quot;]
  },

  &quot;options_page&quot;: &quot;options.html&quot;,
  &quot;permissions&quot;: [

  &quot;history&quot;,
  &quot;storage&quot;,
  &quot;activeTab&quot;,
  &quot;tabs&quot;

huangapple
  • 本文由 发表于 2020年7月24日 21:21:32
  • 转载请务必保留本文链接:https://java.coder-hub.com/63074446.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定