Issue with JavaScript bookmarklet for Gmail

Problem with Gmail bookmarklet in Firefox

I made a bookmarklet to search for unread Gmail messages from today. It works when I open it in a new tab, but if I use it in an existing Gmail tab, it just shows the URL instead of running the script.

Here’s my code:

function searchTodaysUnread() {
  let now = new Date();
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let day = now.getDate();
  
  let searchUrl = `http://mail.google.com/mail/#search/is:unread+after:${year}-${month}-${day}`;
  window.location.href = searchUrl;
}

searchTodaysUnread();

Any ideas why it’s not working in an existing Gmail tab? I’m using Firefox 3. Are there any workarounds or fixes for this issue?

Thanks for your help!

I’ve dealt with similar challenges when creating bookmarklets for Gmail. The issue you’re facing is likely due to Gmail’s single-page application architecture. When you’re already in Gmail, changing window.location doesn’t trigger a full page reload.

One approach that’s worked for me is using Gmail’s URL parameters. Try modifying your bookmarklet to use the ‘#search/’ fragment in the URL:

function searchTodaysUnread() {
  let now = new Date();
  let searchQuery = `is:unread after:${now.getFullYear()/${now.getMonth()+1}/${now.getDate()}`;
  window.location.hash = 'search/' + encodeURIComponent(searchQuery);
}
searchTodaysUnread();

This method should work in both new and existing Gmail tabs. It leverages Gmail’s internal routing system to perform the search without a full page reload.

Also, consider upgrading your Firefox. Version 3 is quite outdated and might lack support for some modern web technologies used by Gmail.

hey, i had a similar problem. try using the gmail api instead of messing with the url. it’s more reliable for this kinda stuff. you’ll need to set up oauth2 and all that, but it’s worth it. here’s a quick example:

gapi.client.gmail.users.messages.list({
  'userId': 'me',
  'q': 'is:unread after:' + new Date().toISOString().split('T')[0]
}).then(function(response) {
  // handle response
});

hope this helps!

I’ve encountered similar issues with bookmarklets in Gmail before. The problem likely stems from Gmail’s use of JavaScript to handle navigation within the app. When you’re already on a Gmail page, the window.location.href method might not trigger a full page reload.

A potential workaround is to use Gmail’s built-in search function instead. Try modifying your bookmarklet to insert the search query directly into Gmail’s search box. Here’s a rough idea:

function searchTodaysUnread() {
  let now = new Date();
  let query = `is:unread after:${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}`;
  document.querySelector('input[name="q"]').value = query;
  document.querySelector('button[aria-label="Search"]').click();
}
searchTodaysUnread();

This approach should work better within an existing Gmail tab. Also, consider updating to a newer Firefox version for better compatibility with modern web apps.