Scriptlets icon indicating copy to clipboard operation
Scriptlets copied to clipboard

Add new scriptlet — 'prevent-click'

Open AdamWr opened this issue 1 year ago • 2 comments

Recently I have noticed that some websites uses click() to open popups. For example:

(() => {
  const url = '/popup';
  const createElm = document.createElement("a");
  createElm.href = url;
  createElm.target = "_blank";
  createElm.click();
  /*
  do something, for example load video player
  */
  console.log('something');
})();

We could add a scriptlet to prevent click if clicked element has specific attribute (like href) with specific content. For example (might need improvements, just an idea):

(() => {
  const preventClick = (element, content) => {
    const wrapper = (target, thisArg, args) => {
      const match = content.split(':');
      const [attribute, text] = match;
      if(thisArg.matches(element) && thisArg[attribute].includes(text)) {
        return;        
      }
      return Reflect.apply(target, thisArg, args);
    };
    const handler = {
      apply: wrapper
    };
    window.HTMLElement.prototype.click = new Proxy(window.HTMLElement.prototype.click, handler);
  };
  // Prevent clicking "a" element if "href" contains "popup"
  preventClick('a', 'href:popup');
})();

AdamWr avatar Sep 02 '24 15:09 AdamWr

preventClick('a', 'href:popup');

Could be one parameter, a selector?

preventClick('a[href*="popup"]');

gorhill avatar Oct 04 '24 19:10 gorhill

Yes, I think that one parameter (selector) should be fine.

AdamWr avatar Oct 05 '24 07:10 AdamWr