Skip to content

Commit 93560da

Browse files
authored
Merge pull request #29 from ut-code/feature/updatesidepanel
拡張機能促し
2 parents a64206c + 2c1b8cb commit 93560da

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/content.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,83 @@
11
// content.tsx
22
export {}
33

4+
// 対象のホスト名
5+
const TARGET_HOST = 'browser-hack.utcode.net';
6+
if (window.location.host === TARGET_HOST) {
7+
let isSidePanelOpen = false;
8+
const warningElement = document.createElement('div');
9+
warningElement.id = 'sidepanel-warning';
10+
warningElement.innerHTML = `
11+
<p><strong>注意</strong></p>
12+
<p>サイドパネルが開かれていません。右上の<span class="badge">ブ</span>をクリックしてサイドパネルを開いてください。</p>
13+
`;
14+
15+
const badge = warningElement.querySelector<HTMLSpanElement>('.badge')
16+
if (badge) {
17+
Object.assign(badge.style, {
18+
display: 'inline-block',
19+
width: '1.6em',
20+
height: '1.6em',
21+
backgroundColor: '#6a6767ff',
22+
textAlign: 'center',
23+
lineHeight: '1.6em',
24+
color: '#ffffff',
25+
})
26+
}
27+
28+
// スタイルを直接設定
29+
warningElement.style.position = 'fixed';
30+
warningElement.style.top = '20px';
31+
warningElement.style.right = '20px';
32+
warningElement.style.padding = '20px';
33+
warningElement.style.backgroundColor = '#fff3cd';
34+
warningElement.style.color = '#ff0000ff';
35+
warningElement.style.border = '1px solid #ffc107';
36+
warningElement.style.borderRadius = '8px';
37+
warningElement.style.zIndex = '9999';
38+
warningElement.style.boxShadow = '0 4px 8px rgba(0,0,0,0.1)';
39+
warningElement.style.fontFamily = 'sans-serif';
40+
warningElement.style.display = 'none'; // 最初は非表示
41+
42+
document.body.appendChild(warningElement);
43+
44+
// 2秒後にサイドパネルが開かれていなければ警告を表示
45+
const warningTimeout = setTimeout(() => {
46+
if (!isSidePanelOpen) {
47+
warningElement.style.display = 'block';
48+
}
49+
}, 2000);
50+
51+
// タブが閉じるとリセット
52+
window.addEventListener('beforeunload', () => {
53+
isSidePanelOpen = false;
54+
clearTimeout(warningTimeout);
55+
});
56+
57+
chrome.runtime.onConnect.addListener((port) => {
58+
if (port.name !== 'sidepanel') return
59+
ports.add(port)
60+
61+
// サイドパネルからのメッセージをリッスン
62+
port.onMessage.addListener((msg) => {
63+
if (msg.type === 'SIDE_PANEL_OPENED') {
64+
isSidePanelOpen = true;
65+
warningElement.style.display = 'none'; // 警告を非表示にする
66+
}
67+
});
68+
69+
const initialValues = getCurrentValues()
70+
lastJson = JSON.stringify(initialValues)
71+
port.postMessage({
72+
type: 'DOM_VALUE_UPDATE',
73+
pageKey: PAGE_KEY,
74+
values: initialValues,
75+
})
76+
77+
port.onDisconnect.addListener(() => ports.delete(port))
78+
})
79+
}
80+
481
const PAGE_KEY = `${location.origin}${location.pathname}`
582

683
function isDisplayed(el: Element | null): boolean {
@@ -79,6 +156,7 @@ chrome.runtime.onConnect.addListener((port) => {
79156
if (port.name !== 'sidepanel') return
80157
ports.add(port)
81158

159+
82160
const initialValues = getCurrentValues()
83161
lastJson = JSON.stringify(initialValues)
84162
port.postMessage({

src/sidepanel/App.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ function App() {
6666

6767
port = chrome.tabs.connect(tab.id, { name: 'sidepanel' })
6868

69+
// --- ここから追加 ---
70+
// サイドパネルが開いたことを content.ts に通知
71+
port.postMessage({ type: 'SIDE_PANEL_OPENED' });
72+
// --- ここまで追加 ---
73+
6974
port.onMessage.addListener((msg) => {
7075
if (msg?.type === 'DOM_VALUE_UPDATE') {
7176
const { p1, p2, p3Visible, p4 } = msg.values

0 commit comments

Comments
 (0)