开发者

How can I block based on URL (from address bar) in a safari extension

开发者 https://www.devze.com 2023-03-14 17:23 出处:网络
I\'m trying to write an extension that will block access to (configurable) list of URLs if they are accessed more than N times per hour. From what I understand, I need to have a start script pass a \"

I'm trying to write an extension that will block access to (configurable) list of URLs if they are accessed more than N times per hour. From what I understand, I need to have a start script pass a "should I load this" message to a global HTML page (who can access the settings object to get the list of URLs), who will give a thumbs up/thumbs down message back to the start script to deny/allow loading.

That works out fine for me, but when I use the usual beforeLoad/canLoad handlers, I get messages for all the sub-items that need to be loaded (images/etc..), which screws up the #accesses/hour limit I'm trying to make.

Is there a way to 开发者_JAVA百科synchronously pass messages back and forth between the two sandboxes so I can tell the global HTML page, "this is the URL in the window bar and the timestamp for when this request came in", so I can limit duplicate requests?

Thanks!


You could use a different message for the function that checks whether to allow the page to load, rather than using the same message as for your beforeLoad handler. For example, in the injected script (which must be a "start" script), put:

safari.self.tab.dispatchMessage('pageIsLoading');

And in the global script:

function handleMessage(event) {
   if (event.name == 'pageIsLoading') {
       if (event.target.url.indexOf('forbidden.site.com') > -1) {
           console.log(event.timeStamp);
           event.target.url = 'about:blank';
       }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消