Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrome 扩展开发中如何关闭侧边栏? #1

Open
Haif-07 opened this issue Oct 23, 2024 · 0 comments
Open

chrome 扩展开发中如何关闭侧边栏? #1

Haif-07 opened this issue Oct 23, 2024 · 0 comments

Comments

@Haif-07
Copy link
Owner

Haif-07 commented Oct 23, 2024

在google 添加chrome.sidePanel.close()接口前,可以通过关闭程序页面的方式关闭sidePanel
该技巧来自(w3c/webextensions#515))
1.在background.js中添加

let isSidePanelOpen = false;

chrome.commands.onCommand.addListener((command) => {
  if (command === "toggle_extension") {
    if (isSidePanelOpen) {
      chrome.runtime.sendMessage('closeSidePanel');
    } else {
      chrome.windows.getCurrent({}, (window) => {
        chrome.sidePanel.open({ windowId: window.id });
      });
    }
    isSidePanelOpen = !isSidePanelOpen; // 切换状态
  }
});

2.修改主页面

<script setup>
import { onMounted } from 'vue';
onMounted(() => {
  chrome.runtime.onMessage.addListener(message => {
 
  if (message === 'closeSidePanel') {
    console.log("Close");
    
    window.close();
  }
})
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant