KM Wiki -- Google Search and Return Markdown Link in Forum Post

PURPOSE:

  • Make it Easy to Find and Get MD Link to KM Wiki Articles
    (most often used in the KM Forum when answering questions).

MACRO:   KM Wiki -- Google Search and Return Markdown Link in Forum Post @KMF

~~~ VER: 2.5    2018-10-16 ~~~

UPDATED: 2018-10-16 16:29 GMT-5
• Fix Chrome Search Page Processing that was broke by Google in latest Chrome Update
• Running Google Chrome 69.0.3497.100 (3497.100) on macOS 10.12.6.

DOWNLOAD:

KM Wiki -- Google Search and Return Markdown Link in Forum Post @KMF.kmmacros (61 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Example Workflow/Output

This whole process takes < 30 seconds, in most cases.

1. Start in Reply to Forum Post

image

2. Enter/Select Search Terms

Type Search Terms
image

OR, Pick from List with Autocomplete
image

3. Choose Link by Number from Search Results

image

4. Choose KM Wiki Article & Return

image

Close Tabs Added in this workflow

5. Paste Markdown Link into Forum Post

image


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Make it Easy to Find and Get MD Link to KM Wiki Articles
    (most often used in the KM Forum when answering questions).
    • Works in Either Safari or Chrome (tested in both)

HOW TO USE:

  1. Create a Forum Reply (or new Topic)
  2. Optionally Select Text as KM Wiki Keyword
  3. Trigger this macro
  4. IF you Selected text,
  • it will set the Wiki Search Term to that selected text
  • ELSE, you will be prompted to enter or select terms from a list
  1. Do a Google Site Search using those terms
  2. Script will Number Google Search Results
  3. Enter Link# on Results Page to open that Wiki Article
    OR
    Press ESC to CANCEL Macro.
  4. Macro will prompt you for the type of return:
  • Markdown Link (default button)
  • Text of Web Page
  • URL of Web Page
  • Check the box for "Set to Clipboard ONLY" to set the clipboard, and NOT paste results.
  1. If you want to goto another Wiki page, do so manually, and when ready,
    click on the "Rescan" button to scan that page in update the KM Prompt.
  2. Click any button (other than "Cancel") to return to your Forum Post and the results will be pasted into the Forum Post.

MACRO SETUP

  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
      .
  • Assign a Trigger to this maro. I prefer ⌃⇧K
  • Move this macro to a Macro Group that is only Active when you need this Macro. I prefer "Browsers"
  • Enable this Macro (if needed).
  • You MUST have enabled/checked "Allow JavaScript from Apple Events" in Safari and Chrome.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magneta color)
    • SET Search URL to Your Preferred Search Provider
      • Default is Google.
    • SET List of Wiki Search Terms
      • Default is list of sections from the KM Home Page

REQUIRES:

  1. Keyboard Maestro 8.2.3+
  2. macOS Yosemite 10.11.3+
  3. Either Safari or Chrome
  4. You MUST have enabled/checked "Allow JavaScript from Apple Events" in Safari and Chrome.

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

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,
    OR IF/THEN and PAUSE Actions

REQUIRES:

  1. Keyboard Maestro Ver 7.3+ (don't even ask me about KM 6 support).
  2. El Capitan 10.11.6+
  • It make work with Yosemite, but I make no guarantees.

USE AT YOUR OWN RISK

  • While I have given this limited testing, and to the best of my knowledge will do no hard, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

4 Likes

Updated with major rewrite just now.

Cool. It fails for me when it tries the close the tab though. What happens is:

  • It opens the google search in a new window
  • Then it opens the wiki page in the same tab, replacing the google search.
  • Then it tries to Close Tab twice, and that fails, since there is no tabs open, and only Close Window is available.

So my guess is you are intending the google search to happen in the same window, but in a new Tab, and then the wiki page to open in another tab, and then to close them both.

This is Safari, with these Tab settings:

I solved it for me by replacing the two Close Tab’s with a single Close Window, but there is probably a more elegant solution somewhere, and clearly that would be bad for you and perhaps others, so a solution that copes with the different behaviours would be better.

Just chiming in to say that I ran into the same problem as Peter, even with different Safari tab settings:

47%20PM

and similarly solved the problem by changing the two "Close Tab" actions to just one. Once I figured that out though, the macro's been working great. Thanks for sharing this!

1 Like

Yep, you nailed it. Evidently I had Safari configured differently from both you and Gabe. I ran fine for me in Safari. I'll have to look at changing it so that it always opens the links in new tab in the same window.

As you guys know I rarely use Safari -- Chrome is my preferred browser.

Sorry for the trouble. I made a bad assumption (actually didn't even think about it) that everyone would have Safari configured like I did for my few tests.

I'll get it fixed.

3 Likes

Confirmed. When Safari is configured like this:

image

the macro runs fine.

I'll fix.

1 Like

Revised version just uploaded.

Fixed the issue with Safari Tabs by changing to New Tab in Front Browser Action, which will work for both Safari and Chrome, and independently of the user's browser settings.

This macro opens two new tabs in the existing window, and then closes them when finished.

2 Likes

Works perfectly, thanks.

1 Like

Just uploaded update.

Apologies for resurrecting this old discussion but...

I've just installed this macro and it seems to be broken. What I get in Step 3 differs insofar as the list of search results is not numbered, so choosing one is not straightforward. See here:

FYI, I'm on Mojave 10.14.6, Safari and KM 9.0.6

Any chance of a fix please @JMichaelTX?

I think Google broke this script some time ago. Here's a replacement JavaScript that is working correctly for me today:

Replace the script in
image

with the below.

// --- REMOVE GOOGLE DICTIONARY ---
var dictElem = document.getElementById("uid_0");
if (dictElem) {
dictElem.parentNode.removeChild(dictElem);
}

// --- REMOVE IMAGES BLOCK ---
var imageElem = document.getElementById("imagebox_bigimages");
if (imageElem) {
  imageElem.parentNode.removeChild(imageElem);
}

//---- REVISED 2018-10-16 to fix for Google changes ---
// --- ADD NUMBERS TO EACH RESULTS LINK ---

//var linkElem = document.querySelectorAll("div.srg div.r a:not([class])");
var linkElem = document.querySelectorAll("div.r>a:not([class])");

var numElements = linkElem.length;
for (var iElem = 0; iElem < numElements; iElem++) {

  linkElem[iElem].innerText = (iElem + 1) + ". " + linkElem[iElem].innerText;

} // END for linkElem


1 Like

Thank you @JMichaelTX - I’ll try that later today :+1:

That solves the missing numbering of the search results but there is now another problem: the script to populate the variable Local__WikiURL returns nothing and leaves the variable empty and the new browser tab is an empty page.

This is the sequence:
KM 0 2020-09-05_12-43-44

(Ignore the Testing Only action group - I put that in to find the problem.)

I'm not clever enough to figure out a fix for that myself.

OK, it sounds like I need to upload an updated version of the entire macro.

Let me review and I'll get back to you.

1 Like

Hi @JMichaelTX, any thoughts on when you might be able to fix this?

Sorry, @tiffle, it could be a while. I've got several major projects active at the moment.

If I had the expertise I’d have a go at fixing this myself but I don’t :frowning:

Maybe you could make a note at the top to say it’s broken at present so others don’t waste their time trying the macro?

Cheers.