如何编写Tampermonkey脚本修改按钮onclick事件URL以下载STP文件
Got it, let's tweak that "Open in Fusion360" button so it lets you download the STP file directly instead of launching the Fusion360 protocol. Here's a ready-to-use Tampermonkey script that handles all the heavy lifting automatically:
// ==UserScript== // @name Direct STP Download from Fusion360 Button // @namespace http://tampermonkey.net/ // @version 0.1 // @description Turns the "Open in Fusion360" button into a direct STP file download button // @author Your Name // @match https://your-target-website.com/* // Replace with the actual site domain // @grant none // ==/UserScript== (function() { 'use strict'; // Wait for the button to load (handles dynamic page content) const buttonChecker = setInterval(() => { const fusionButton = document.querySelector('button.event.btn.btn-default.btn-lg.btn-block[onclick*="fusion360://command=insert"]'); if (fusionButton) { clearInterval(buttonChecker); // Extract the raw STP URL from the button's onclick code const onclickText = fusionButton.getAttribute('onclick'); const stpUrlMatch = onclickText.match(/encodeURIComponent\("(.*?)"\)/); if (stpUrlMatch && stpUrlMatch[1]) { const stpFileUrl = decodeURIComponent(stpUrlMatch[1]); // Update the button to point directly to the STP file fusionButton.setAttribute('onclick', `window.location.href = "${stpFileUrl}";`); // Optional: Update button text to reflect the new function fusionButton.textContent = 'Download STP File'; // Optional: Update accessibility label fusionButton.setAttribute('aria-label', 'Download STP File'); } } }, 500); // Check every 500ms until the button is found })();
Key Details & Setup Steps:
- Update the @match line: Replace
https://your-target-website.com/*with the actual domain of the site where this button appears. This ensures the script only runs where it's needed. - Dynamic content handling: The script uses
setIntervalto wait for the button to load, which works even if the button is added to the page dynamically (like via AJAX). - URL extraction: Instead of hardcoding the STP URL, it pulls the original link straight from the button's existing onclick code—so it'll work for any similar button on the site, not just the specific one you shared.
- Optional tweaks: The script includes lines to change the button text and accessibility label, but you can comment those out if you prefer to keep the original button text.
How to Use:
- Install the Tampermonkey extension for your browser (Chrome, Firefox, Edge, etc.).
- Click the Tampermonkey icon and select "Create a new script".
- Replace the default template with the code above.
- Update the
@matchline to match your target site. - Save the script (Ctrl+S or Cmd+S).
- Refresh the page with the Fusion360 button—you'll now see it lets you download the STP file directly when clicked!
内容的提问来源于stack exchange,提问作者pball
相关产品推荐
相关产品推荐

