Chrome中日期类Bookmarklet无法正常工作,请求技术协助
Fixing Your Blue Apron Grocery Board Bookmarklet
Let's walk through the issues in your bookmarklet and get it working properly:
Key Issues in the Original Code
- Incorrect HTML Entity: You used
&in the URL string, which is an HTML escape sequence. In JavaScript bookmarklets, we need to use the literal&for URL parameters—using&will result in an invalid parameter name (amp;week_starts_on) that the server won't recognize. - Duplicate Variable Name: You declared
var datetwice in the same scope. While JavaScript allows this, it's messy and can lead to confusion down the line. - (Minor) Redundant Date Logic: The date formatting works, but we can clean it up a bit for better readability without changing functionality.
Fixed Bookmarklet Code
javascript:(function() { const now = new Date(); // Calculate next Monday (or today if it's already Monday) const daysToAdd = (1 + 7 - now.getDay()) % 7; now.setDate(now.getDate() + daysToAdd); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); const targetDate = `${year}-${month}-${day}`; const targetUrl = `https://wms.blueapron.com/facilities/4/grocery-board?view_type=cumulative&week_starts_on=${targetDate}`; window.open(targetUrl, "_blank"); })();
What Changed?
- Wrapped the code in an IIFE (Immediately Invoked Function Expression) to avoid polluting the global scope—this is a standard best practice for bookmarklets.
- Renamed the initial date variable to
nowto avoid duplication with the formatted date string. - Replaced
&with the literal&to ensure the URL parameters are parsed correctly by the server. - Used
padStart()for cleaner date formatting (works in all modern browsers; if you need support for very old browsers, you can revert to your originalifcheck logic). - Used template literals for more readable string concatenation instead of manual
+joins.
How to Use
- Create a new bookmark in your browser.
- Replace the bookmark's URL with the fixed code above.
- Click the bookmark—it should open the Blue Apron Grocery Board for the next (or current) Monday as intended.
内容的提问来源于stack exchange,提问作者Brandon Hunt
相关产品推荐
相关产品推荐

