Chrome 扩展程序切换按钮可打开和关闭我的弹出窗口

Chrome extension toggle button to switch on and off my popup

提问人:Benz Tagle 提问时间:4/5/2023 更新时间:4/5/2023 访问量:195

问:

我在弹出窗口中制作了一个切换按钮,从popup_0.html(默认弹出窗口)切换到popup_1.html。如果切换开关关闭:使用popup_0.html。如果切换开关打开:popup_1.html即使用。

我的问题是弹出窗口的切换不是即时的,弹出窗口仍然需要关闭并重新打开才能看到新的弹出窗口。我怎样才能对此进行编码,使弹出窗口切换即时而不需要关闭和重新打开?我使用了 setPopup,我什至应该考虑在 html 中使用 a 标签的切换吗?

这是我的 chrome 扩展程序的打开和关闭按钮。我是开发 chrome 扩展程序的新手,请帮帮我!:)

popup_0.html:

<label class="switch">
        <input type="checkbox">
        <span class="slider">
         <svg class="slider-icon" viewBox="0 0 32 32" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="presentation"><path fill="none" d="m4 16.5 8 8 16-16"></path></svg> 
        </span>
      </label>

弹出式.js:

const slider = document.querySelector('.slider');
const checkbox = document.querySelector('input[type="checkbox"]');

function setSliderBackground(checked) {
  slider.style.backgroundColor = checked ? 'green' : '#B0B0B0';
}

function setPopupHtml(checked) {
  const popupHtml = checked ? 'popup_1.html' : 'popup_0.html';
  chrome.action.setPopup({ popup: popupHtml });
}

function toggleCheckbox() {
  const checked = checkbox.checked;
  setSliderBackground(checked);
  setPopupHtml(checked);
  chrome.storage.sync.set({ checked });
}

function loadToggleStateFromStorage() {
  chrome.storage.sync.get('checked', ({ checked }) => {
    checkbox.checked = checked;
    setSliderBackground(checked);
    setPopupHtml(checked); // Set the popup HTML after the toggle state is loaded
  });
}

checkbox.addEventListener('change', toggleCheckbox);
loadToggleStateFromStorage();

CSS格式:


.switch {
  font-size: 17px;
  position: relative;
  display: inline-block;
  width: 3em;
  height: 1.5em;
  left: 2em;

}

/* Hide default HTML checkbox */
.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

/* The slider */
.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #B0B0B0;
  border: 1px solid #B0B0B0;
  transition: .4s;
  border-radius: 32px;
  outline: none;
  box-shadow: inset 3px 3px 5px rgba(0,0,0);

}

.slider:before {
  position: absolute;
  content: "";
  height: 1.5rem;
  width: 1.5rem;
  border-radius: 50%;
  outline: 2px solid #B0B0B0;
  left: -1px;
  bottom: -1px;
  background-color: #fff;
  transition: transform .25s ease-in-out 0s;
}


input:checked + .slider {
  background-color: #0C0B37;
}

input:checked + .slider .slider-icon {
  opacity: 1;
  right: 20%;
}

input:checked + .slider:before {
  transform: translateX(1.5em);
  outline-color: #181818;
}


javascript 浏览器 谷歌浏览器扩展 程序 google-chrome-app chrome-extension-manifest-v3

评论

0赞 wOxxOm 4/5/2023
在弹出脚本中使用。location.href='newPopup.html'

答: 暂无答案