Action
Open Link
Posted by sylumer,
Last update
about 4 years ago
- Unlisted
Open the link under the cursor.
(Not wiki links with this version)
Steps
-
script
function openURLUnderCursor() { let strURL = grabURL(); if(strURL == undefined) app.displayErrorMessage("No valid URL found at cursor"); else app.openURL(strURL); } function validateURL(p_strURL) { let httpChecker = HTTP.create(); let respChecker = httpChecker.request( { "url": p_strURL, "method": "GET" }); if (respChecker.success) return true; else return false; } function isEnder(p_strInput) { return (!p_strInput || p_strInput.length === 0 || /^\s*$/.test(p_strInput) || p_strInput == "\n") } function grabURL() { //Initialise the starting position let aintSel = editor.getSelectedRange(); //Initialise the start and end markers let intStart = aintSel[0]; let intEnd = aintSel[0]; //Initialise the start and end characters let strStart = editor.getTextInRange(intStart,1); let strEnd = editor.getTextInRange(intEnd,1); //Extend back to the first whitespace, new line, or start of content while (intStart > 0 && !isEnder(strStart)) { intStart--; strStart = editor.getTextInRange(intStart,1); } intStart++; //Extend forward to the first whitespace, new line, or end of content while (intEnd < editor.getText().length && !isEnder(strEnd)) { intEnd++; strEnd = editor.getTextInRange(intEnd, 1); } //Grab what we've narrowed it down to let strWorking = editor.getTextInRange(intStart, intEnd - intStart); //If the selected text contains double quotes, then we can reasonably assume that we //are dealing with a URL contained in double quotes. if(strWorking.includes('"')) strWorking = strWorking.split('"')[1]; //Likewise single quotes. else if(strWorking.includes("'")) strWorking = strWorking.split("'")[1]; //If it is a markdown link we need to work a bit smarter else if(strWorking.endsWith(")")) { let intFrom = strWorking.lastIndexOf("(") + 1; let intTo = strWorking.length - 1; strWorking = strWorking.substring(intFrom, intTo); } if(strWorking.includes("://")) return strWorking; if(validateURL(strWorking)) return strWorking; if(validateURL("https://" + strWorking)) return strWorking; if(validateURL("http://" + strWorking)) return strWorking; return; } openURLUnderCursor();
Options
-
After Success Default Notification Error Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.