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

Add new scriptlet — 'prevent-click' #449

Open
AdamWr opened this issue Sep 2, 2024 · 2 comments
Open

Add new scriptlet — 'prevent-click' #449

AdamWr opened this issue Sep 2, 2024 · 2 comments
Assignees
Labels

Comments

@AdamWr
Copy link
Member

AdamWr commented Sep 2, 2024

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');
})();
@slavaleleka slavaleleka added the Feature request Adding new feature label Sep 27, 2024
@adguard-bot adguard-bot changed the title Add prevent-click scriptlet Add new scriptlet — 'prevent-click' Sep 27, 2024
@gorhill
Copy link
Contributor

gorhill commented Oct 4, 2024

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

Could be one parameter, a selector?

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

@AdamWr
Copy link
Member Author

AdamWr commented Oct 5, 2024

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

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

No branches or pull requests

5 participants