Browse Source

WIP: working on popup menu button functionality

Brandon Wong 1 year ago
parent
commit
19eaaaa5f1
7 changed files with 193 additions and 20 deletions
  1. 141 0
      2025-04-01-7-claude-convert-play-button-to-link.md
  2. 1 1
      background.js
  3. 1 0
      content_scripts/content.js
  4. 0 5
      manifest.json
  5. 5 2
      popup/popup.css
  6. 16 11
      popup/popup.html
  7. 29 1
      popup/popup.js

+ 141 - 0
2025-04-01-7-claude-convert-play-button-to-link.md

@@ -0,0 +1,141 @@
+## Me
+
+> Sharing:
+> - 👀 <buf>popup/popup.html</buf>
+> - 👀 <buf>popup/popup.css</buf>
+> - <tool>files</tool>
+
+@files modify this CSP-friendly alpine.js page so that the play-btn is a link instead of a button, but still looks the same (still looks like a button), and opens video.url instead of calling a function.
+
+## CodeCompanion (Anthropic)
+
+### Reasoning
+
+I need to modify the HTML to change the play button from a button element that calls a function to a link (anchor tag) that directly opens the URL, while still maintaining the same appearance. Let's analyze the relevant parts of the code:
+
+Current implementation (from `popup.html`):
+```html
+<button @click="openVideo(video.url)" class="play-btn">
+  ⏵
+</button>
+```
+
+This button currently:
+1. Has a class of "play-btn"
+2. Calls the `openVideo(video.url)` function when clicked
+3. Shows a "⏵" (play triangle) character
+
+For the CSS, the relevant styles are:
+```css
+button {
+    padding: 4px 8px;
+    border: none;
+    border-radius: 4px;
+    cursor: pointer;
+    font-size: 12px;
+}
+
+.play-btn {
+    background-color: #4285f4;
+    color: white;
+}
+
+button:hover {
+    opacity: 0.9;
+}
+```
+
+To change this to a link while maintaining the same appearance, I need to:
+1. Replace the `<button>` with an `<a>` tag
+2. Set the href attribute to the video URL
+3. Add CSS to make the link look like the button
+
+Here's the plan for the changes:
+
+1. In the HTML, change the button to an anchor tag:
+```html
+<a :href="video.url" class="play-btn">
+  ⏵
+</a>
+```
+
+2. Add CSS to make the anchor tag look like the button. I'll need to add styles for the `a.play-btn` selector to match the button styles.
+
+Let me create the necessary XML to make these changes:
+
+### Response
+
+Looking at your Alpine.js page, I'll modify the play button to be a link while maintaining its button-like appearance. Here's how:
+
+```xml
+<tools>
+  <tool name="files">
+    <action type="edit">
+      <path>/home/brwong/projects/experiments/playlist/popup/popup.html</path>
+      <search><![CDATA[                      <button @click="openVideo(video.url)" class="play-btn">
+                        ⏵
+                      </button>]]></search>
+      <replace><![CDATA[                      <a :href="video.url" target="_blank" class="play-btn">
+                        ⏵
+                      </a>]]></replace>
+    </action>
+    <action type="edit">
+      <path>/home/brwong/projects/experiments/playlist/popup/popup.css</path>
+      <search><![CDATA[button {
+    padding: 4px 8px;
+    border: none;
+    border-radius: 4px;
+    cursor: pointer;
+    font-size: 12px;
+}]]></search>
+      <replace><![CDATA[button, a.play-btn {
+    padding: 4px 8px;
+    border: none;
+    border-radius: 4px;
+    cursor: pointer;
+    font-size: 12px;
+}]]></replace>
+    </action>
+    <action type="edit">
+      <path>/home/brwong/projects/experiments/playlist/popup/popup.css</path>
+      <search><![CDATA[.play-btn {
+    background-color: #4285f4;
+    color: white;
+}]]></search>
+      <replace><![CDATA[.play-btn {
+    background-color: #4285f4;
+    color: white;
+    text-decoration: none;
+    display: inline-block;
+    text-align: center;
+}]]></replace>
+    </action>
+    <action type="edit">
+      <path>/home/brwong/projects/experiments/playlist/popup/popup.css</path>
+      <search><![CDATA[button:hover {
+    opacity: 0.9;
+}]]></search>
+      <replace><![CDATA[button:hover, a.play-btn:hover {
+    opacity: 0.9;
+}]]></replace>
+    </action>
+  </tool>
+</tools>
+```
+
+These changes:
+1. Replace the button with an anchor tag that links directly to `video.url`
+2. Add `target="_blank"` to open links in a new tab
+3. Update the CSS selectors to apply button styling to the play-btn link
+4. Add additional properties (text-decoration, display, text-align) to make the link appear like a button
+
+Would you like me to explain any specific part of these changes in more detail?
+
+## Me
+
+> Sharing:
+> - 👀 <buf>popup/popup.html</buf>
+> - 👀 <buf>popup/popup.css</buf>
+> - <tool>files</tool>
+
+

+ 1 - 1
background.js

@@ -262,7 +262,7 @@ async function navigateAndWait(tabId, url) {
     const listener = (details) => {
       if (details.tabId === tabId && details.frameId === 0) {
         browser.webNavigation.onCompleted.removeListener(listener);
-        resolve();
+        setTimeout(resolve, 500);
       }
     };
 

+ 1 - 0
content_scripts/content.js

@@ -35,6 +35,7 @@
       console.log("MESSAGE", message);
     }
     if (message.type === "autoplay") {
+      console.log('press play');
       const play = document.querySelector(
         'button.ytp-play-button.ytp-button[data-title-no-tooltip="Play"]',
       );

+ 0 - 5
manifest.json

@@ -3,10 +3,6 @@
   "name": "My Playlist Extension",
   "version": "1.0",
   "description": "An extension to manage youtube videos",
-  "icons": {
-    "48": "icons/icon-48.png",
-    "96": "icons/icon-96.png"
-  },
   "permissions": [
     "activeTab",
     "tabs",
@@ -17,7 +13,6 @@
     "<all_urls>"
   ],
   "browser_action": {
-    "default_icon": "icons/icon-48.png",
     "default_title": "My Playlist Extension",
     "default_popup": "popup/popup.html"
   },

+ 5 - 2
popup/popup.css

@@ -66,7 +66,7 @@ h2 {
     gap: 8px;
 }
 
-button {
+button, a.play-btn {
     padding: 4px 8px;
     border: none;
     border-radius: 4px;
@@ -77,6 +77,9 @@ button {
 .play-btn {
     background-color: #4285f4;
     color: white;
+    text-decoration: none;
+    display: inline-block;
+    text-align: center;
 }
 
 .remove-btn {
@@ -84,7 +87,7 @@ button {
     color: white;
 }
 
-button:hover {
+button:hover, a.play-btn:hover {
     opacity: 0.9;
 }
 

+ 16 - 11
popup/popup.html

@@ -20,30 +20,35 @@
           :key="playlistName"
         >
           <div class="playlist">
-            <h2 x-text="formatPlaylistName(playlistName)"></h2>
+            <h2 x-text="playlistName"></h2>
             <div class="video-list">
               <template x-for="(video, index) in videos" :key="index">
-                <div class="video-item" :title="video.url">
-                  <span
-                    class="video-title"
-                    x-text="video.title || 'Untitled Video'"
-                  ></span>
+                <div class="video-item" :title="video.title">
+                  <span class="video-title" x-text="video.title" x-bind="videotitle"></span>
                   <div class="video-actions">
-                    <button @click="openVideo(video.url)" class="play-btn">
-                      Play
-                    </button>
+                    <a
+                      :href="video.url"
+                      class="play-btn"
+                      x-bind="videoPlayLink"
+                    >
+                      ⏵
+                    </a>
                     <button
-                      @click="removeVideo(playlistName, index)"
+                      :playlistName="playlistName"
+                      :playlistIndex="index"
+                      x-bind="removeVideoButton"
                       class="remove-btn"
                     >
-                      Remove
+                      X
                     </button>
                   </div>
                 </div>
               </template>
+              <!--
               <div x-show="videos.length === 0" class="empty-playlist">
                 No videos in this playlist
               </div>
+              -->
             </div>
           </div>
         </template>

+ 29 - 1
popup/popup.js

@@ -1,4 +1,11 @@
 document.addEventListener("alpine:init", () => {
+  //TODO
+  // - reorder videos in playlist
+  // - remove videos from playlist
+  // - preserve playlist order (sort)
+  // - "play/resume playlist" button (pick up where you left off)
+  //   - track timestamp to truly resume where you left off
+  // - periodically remove watched videos
   Alpine.data("playlistManager", () => ({
     playlists: {},
 
@@ -49,6 +56,27 @@ document.addEventListener("alpine:init", () => {
         console.error("Error removing video:", error);
       }
     },
+
+    videotitle: {
+      ["@click"]() {
+        console.log('TITLE CLICK', this.$el);
+      },
+    },
+
+    videoPlayLink: {
+      ["@click.prevent"](evt) {
+        if (evt.target && evt.target.href) {
+          browser.tabs.update({ url: evt.target.href });
+        }
+      },
+    },
+
+    removeVideoButton: {
+      ["@click"](evt) {
+        if (evt.target && evt.target.attributes.playlistName && evt.target.attributes.playlistIndex) {
+            this.removeVideo(evt.target.attributes.playlistName.value, evt.target.attributes.playlistIndex.value);
+        }
+      },
+    },
   }));
 });
-console.log("popup.js FINISHED doing something");