I want to get the url of window.open() implemented in the site by browser extension before opening it.

Asked 1 years ago, Updated 1 years ago, 478 views

The whole thing you want to do

In order to prevent transitions to phishing sites by notifications in the site, if notifications using Notification api are detected in the site, the title and body contents of the notification, and the url of the transition destination are obtained.

What questions would you like to ask

·Before opening the window.open() implemented in the site, I want to pick it up in the browser extension and get url.

Implemented to the point where the previous question obtained arguments in the form of overrides of the Notification constructor, and is now available for notificationAttempting to get url for transition destination.I was able to get url after transition by clicking notification, but the purpose is to block access to phishing sites, so I would like to get url before transition.

Notification.js

implemented on the website side
function showNotification(){
    const notify = new Notification("Hither", {
      body: "Notification test.",
      icon: "assets/notifications.png"
    });

    notify.onclick=(e)=>{
      // window.location.href="https://github.com/"; // redirect the page
      window.open('https://github.com/','_blank'); // Open on a separate tab
    };
  }

manifest.json

{
  "name": "Blocking fishing sites",
  "description": "Control method for web push notification by browser extension",
  "version": "1.0",
  "manifest_version"—2,
  "web_accessible_resources": ["js/content.js",
  "content_scripts":[
    {
      "matches": ["<all_urls>",
      "run_at": "document_start",
      "js": ["js/inject.js" ]
    }
  ],
  "permissions": [
    "*://*/*",
    "tabs",
    "webRequest",
    "webRequestBlocking",
    "webNavigation"
  ]
}

content.js

Notification=(function(Notification){

  function MyNotification (...args) {
    console.log("title:", args[0]);
    console.log("body:", args[1].body);
    console.log("icon:", args[1].icon);

        ...(omitted) ...
    return new Notification (...args);

  };

  Object.assign (MyNotification, Notification);
  MyNotification.prototype =Notification.prototype;

  return MyNotification;
}) (Notification);

window.open=function(open){
  return function(url, name, features) {
      console.log("url:", url);
      return open.call(window, url, name, features);
  };
}(window.open);

content.js is the ... (omitted) section, where you blacklist the title or body and run return new Notification (...args);

underneath it if it is not registered.

I would appreciate it if you could reply when you have time.

javascript google-chrome chrome-extension

2022-11-11 18:56

1 Answers

I was able to get url after the transition

However, it seems that the code of the question was obtained before the transition.I immediately called the original (before replacement) window.open, so I think it's just that I don't know the order.

Wouldn't it work if I use the url that I got as it is and branch the conditions?

 if(url===BAD_URL){
    // rejection
    return null;
} else{
    // Invoke original window.open
    return open...
}


2022-11-12 00:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.