Chrome 扩展选项

发布于 2024-11-07 16:17:53 字数 2282 浏览 0 评论 0原文

好的,我正在尝试为我的 Chrome 扩展程序设置选项。我不知道为什么会失败,但这是我的代码。我确信我做错了什么。

options.html

<html>
<head><title>PT'd Settings</title></head>
<script type="text/javascript">

// Saves options to localStorage.
function save_options() {
  var select = document.getElementById("show");
  var option = select.children[select.selectedIndex].value;
  localStorage["show"] = option;

  // Update status to let user know options were saved.
  var status = document.getElementById("status");
  status.innerHTML = "Options Saved: "+localStorage["show"]; //this shows up perfectly
  setTimeout(function() {
    status.innerHTML = "";
  }, 750);
}

// Restores select box state to saved value from localStorage.
function restore_options() {
  var favorite = localStorage["show"];
  if (!favorite) {
    return;
  }
  var select = document.getElementById("show");
  for (var i = 0; i < select.children.length; i++) {
    var child = select.children[i];
    if (child.value == favorite) {
      child.selected = "true";
      break;
    }
  }
}

</script>

<body onload="restore_options()">

When to show image:
<select id="show">
 <option value="load">Show on Load</option>
 <option value="click">Show on Click</option>
</select>

<br>
<button onclick="save_options()">Save</button>
<br />
<div id="status"></div>
</body>
</html>

然后我需要将该选项发送到我

javascript

<script type="text/javascript">
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {set: localStorage["show"]}, function(response) {
        document.getElementById("box").value = response.answer; //just for testing purposes
    });
});
document.write(localStorage["show"]); //just for testing purposes
</script>
<div id="box"></div>

注入

...
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    alert("set "+request.set); //never even runs
    if(request.show == "click") {
        //calculate "found" value and send it back
        sendResponse({answer: found});
    }
});
...

的 em>任何 JavaScript 变量。

谢谢

Okay so I'm trying to set options for my Chrome extension. I don't know why this is failing, but here's my code. I'm sure I'm doing something wrong.

options.html

<html>
<head><title>PT'd Settings</title></head>
<script type="text/javascript">

// Saves options to localStorage.
function save_options() {
  var select = document.getElementById("show");
  var option = select.children[select.selectedIndex].value;
  localStorage["show"] = option;

  // Update status to let user know options were saved.
  var status = document.getElementById("status");
  status.innerHTML = "Options Saved: "+localStorage["show"]; //this shows up perfectly
  setTimeout(function() {
    status.innerHTML = "";
  }, 750);
}

// Restores select box state to saved value from localStorage.
function restore_options() {
  var favorite = localStorage["show"];
  if (!favorite) {
    return;
  }
  var select = document.getElementById("show");
  for (var i = 0; i < select.children.length; i++) {
    var child = select.children[i];
    if (child.value == favorite) {
      child.selected = "true";
      break;
    }
  }
}

</script>

<body onload="restore_options()">

When to show image:
<select id="show">
 <option value="load">Show on Load</option>
 <option value="click">Show on Click</option>
</select>

<br>
<button onclick="save_options()">Save</button>
<br />
<div id="status"></div>
</body>
</html>

Then I need to send that option to my injected javascript

background.html

<script type="text/javascript">
chrome.tabs.getSelected(null, function(tab) {
    chrome.tabs.sendRequest(tab.id, {set: localStorage["show"]}, function(response) {
        document.getElementById("box").value = response.answer; //just for testing purposes
    });
});
document.write(localStorage["show"]); //just for testing purposes
</script>
<div id="box"></div>

injected javascript

...
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    alert("set "+request.set); //never even runs
    if(request.show == "click") {
        //calculate "found" value and send it back
        sendResponse({answer: found});
    }
});
...

So I'm not even sure if the background.html page can read the localStorage, but I do know for a fact that it's not properly sending any variables to the javascript.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-11-14 16:17:53

我不确定我完全明白你在做什么,但是......也许“getBackgroundPage" 方法可能会点亮灯泡?

getBackgroundPage() 允许您在扩展页面之间共享 JS 状态(如果您使用过 Node 或 requireJS,则有点像模块)...我在 popup.htm 中使用它来获取和显示我的 background.htm 具有的任何数据集。在 background.htm 中,我通过扩展徽章和桌面通知提醒用户。

希望有帮助!

I'm not sure I quite get what you are up to but... perhaps the "getBackgroundPage" method might set off a light bulb?

getBackgroundPage() allows you to share JS state between your extension pages (sort of like a module if you've used node or requireJS)... I use it in my popup.htm to grab and display any data my background.htm has collected. From background.htm I am alerting the user via extension badge and desktopnotifications.

Hope it helps!

淡莣 2024-11-14 16:17:53

我想通了!

只需向清单添加权限

"permissions": ["tabs"]

Google Chrome 的文档有点薄弱。

I figured it out!

Just had to add permissions to the manifest

"permissions": ["tabs"]

Google Chrome's documentation is kind of weak.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文