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.
·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 sidefunction 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);
I would appreciate it if you could reply when you have time.
javascript google-chrome chrome-extension
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...
}
© 2024 OneMinuteCode. All rights reserved.