Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
vollkorn1982 opened this issue Apr 13, 2024 · 2 comments

Comments

@vollkorn1982
Copy link

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?

@vollkorn1982
Copy link
Author

btw: Same problem for the openFn function, but that's not the one I was looking at in the debugger.

@FeSuert
Copy link

FeSuert commented Apr 29, 2024

I believe my pull request #57 directly addresses this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants