MACRO: @WEB Extract & Process Links on Web Page Using HTML Class [Example]
VER: 1.1 2016-12-26
DOWNLOAD:
@WEB Extract & Process Links on Web Page Using HTML Class [Example].kmmacros (45 KB)
SubMacros Used
This macro uses (but does not require) this SubMacro:
@LINK Process a Web Page Hyperlink @SubMacro.kmmacros (28.6 KB)
It is provided as an example of how you can use submacros with this macro.
Be sure to read the Macro Setup in the Release Notes section below.
Use Case
-
Make it easy for most users, most use cases, to extract all hyperlinks in a list on a web page, and then process each link.
-
None of which requires the user to understand or change JavaScript.
-
Most often, these list of links will either be within a major HTML element with a unique Class, or each link will be within an element that has the same Class for all of these elements.
-
You can easily find this HTML element, and its Class, by using the Inspector in either Chrome or Safari.
-
This method/macro won't work in all cases, but hopefully it will in most cases.
-
If it does not work for you, we can probably figure out a method that will. Just post the URL of the target page.
Example Results
ReleaseNotes
Author.@JMichaelTX
PURPOSE:
- Extract Web Page Links Using HTML Class, and Process Each Link
MACRO SETUP
Note that all Actions with the magenta color are designed to be changed by you.
In some cases, they MUST be changed to fit your specific requirements.
- Move Macro to Macro Group that limits trigger to apps you plan to use it with
- (Note: This macro can be used ONLY with Google Chrome, but could be easily changed to use Safari, just by replacing the Chrome Actions with Safari Actions)
- Assign a Trigger
- Set the below Action "SET Source URL" to the URL of the Web Page that contains the list of links.
- Set the below Action "SET HTML Class" to the unique Class of the HTML Element that contains each, or all, of the list of links.
- ADD Actions at the bottom of the Macro to process each link as you desire.
- If your web page has a lot of links, it is best to first TEST on a similar page with just a few links)
HOW TO USE:
- Open Google Chrome Browser (any page)
- Trigger this Macro
WHAT IT DOES:
- Gets a HTML Collection of all Elements that have the specified Class Name
- Gets a HTML Collection of all Links (Anchor Tags) within that collection
- Builds a TAB delimited list (array) of Link Text & URL from that collection
- Returns a TAB delimited String, with each link on a separate line
- FOR EACH link/line in that String:
- Using RegEx, parses it into Title and URL
- Process that Link
TAGS: @Links @Web @JavaScript @HTML
USER SETTINGS:
- Any Action in magenta color is designed to be changed by end-user
- This macro uses Google Chrome, but can be easily changed
ACTION COLOR CODES
-
To facilitate the reading, customizing, and maintenance of this macro,
key Actions are colored as follows: -
GREEN -- Key Comments designed to highlight main sections of macro
-
MAGENTA -- Actions designed to be customized by user
-
YELLOW -- Primary Actions (usually the main purpose of the macro)
-
ORANGE -- Actions that permanently destroy Varibles or Clipboards
REQUIRES:
(1) Keyboard Maestro Ver 7.3+
(2) Yosemite (10.10.5)+
JavaScript
'use strict';
(function run() { // function will auto-run when script is executed
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var ptyScriptName = "Extract Link List Using Class Name"
var ptyScriptVer = "1.1"
var ptyScriptDate = "2016-12-26"
var ptyScriptAuthor = "@JMichaelTX"
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var returnResults = "TBD"
//--- Get Class Name from KM of Major HTML Elements Which Contain the Link ---
var classStr = document.kmvar.ELP__HTMLClass
//--- IF KM VAR "ELP__HTMLClass" IS EMPTY, RETURN ERROR ---
if (!classStr) {
returnResults = "[ERROR]\n\n"
+ "SCRIPT: " + ptyScriptName + " Ver: " + ptyScriptVer + "\n"
+ "Error Number: " + "C101" + "\n"
+ "Invalid HTML Class:" + ">>>" + classStr + "<<<"
+ "\n Must be set in KM Variable: 'ELP__HTMLClass'"
}
else {
//--- Get Element Collection Using Class Name ---
var majorElem = document.getElementsByClassName(classStr);
//--- Get Links (Anchor Elements) Within the majorElem Collection ---
var linkElem = majorElem[0].getElementsByTagName("a")
//--- Build TAB Delimited List of Link Text and URL ---
var linkList = [];
var numElements = linkElem.length;
for (var iElem = 0; iElem < numElements; iElem++) {
linkList.push(linkElem[iElem].textContent + "\t" + linkElem[iElem].href);
}
returnResults = linkList.join("\n");
} // END of else
//--- Merge Link List into String using LineFeed, & Return to KM --
return returnResults;
})();