Skip to content

OnContextMenu for data-term links will always set link of last link in document to all links #56

@vollkorn1982

Description

@vollkorn1982

In

this.setAttribute("href", link);
is the culprit:

export function registerPortHandlers(termName, instance) {
  var self = this;
  // Attach block actions
  var actions = document.querySelectorAll('[data-term*="' + termName + '"]');

# This will iterate through all links
  for (var n = 0; n < actions.length; ++n) {
    var anchor = actions[n];
    var port = anchor.getAttribute("data-port");
    var protocol = anchor.getAttribute("data-protocol") || "http:";
    var link;
    if (port) {
      link =
        protocol +
        "//" +
        instance.proxy_host +
        "-" +
        port +
        ".direct." +
        self.opts.baseUrl.split("/")[2] +
        anchor.getAttribute("href");
    }
    var openFn = function (link) {
      return function (evt) {
        evt.preventDefault();
        if (link) {
          window.open(link, "_blank");
        }
      };
    };
    anchor.addEventListener("click", function () {
      openFn(link);
    });
    // anchor.onauxclick = openFn(link);
    anchor.addEventListener("contextmenu", function () {

# since this is an anonymous function, "link" will not be evaluated yet, but instead it will be called with the value of "link" as it was left in memory after processing the last a-tag in the above loop
      if (link) {
        this.setAttribute("href", link);
      }
    });
  }
}

I would build this mechanism by taking the function to build the links from the attributes and attach them to the listeners, so they are run every time a click or right click is done. This will ensure the function will only operate on the link's attributes it is attached to. The main function then would only attach the listeners, but do not yet evaluate the links. What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions