如何在Google Chrome扩展程序中阻止iframe [英] How to block iframes in Google Chrome Extension

查看:345
本文介绍了如何在Google Chrome扩展程序中阻止iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google Chrome可以选择禁用flash和java,并且只能在用户点击时运行它们,如何创建将执行此操作的扩展?

Google Chrome have option to disable flash and java and only run them on user click, how to create extension that will do this?

推荐答案

您可以使用 onBeforeRequest 事件 webRequest API 。使用类型创建一个过滤器:['sub_frame'] 和extraInfoSpec ['blocking'] 。然后,在事件侦听器中返回 {cancel:true}

You can implement this feature using the onBeforeRequest event of the webRequest API. Create a filter with type: ['sub_frame'], and extraInfoSpec ['blocking']. Then, return {cancel:true} in the event listener.

最小示例:

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    // Save the data in `details` for later use
    // The data must be associated with the `tabId` and `frameId`, so that it
    //  can be used later
    if (your_method_says_block_it())
       return {cancel: true};
}, {
    urls: ['*://*/*'],
    types: ['sub_frame']
}, ['blocking']);

清单文件:

 ...
    "permissions": ["webRequest", "webRequestBlocking", "*://*/*"]
 ...




  • webRequest 需要权限才能启用API。
  • 需要 webRequestBlocking 权限才能使请求处理同步,以启用该功能修改(取消)请求。

  • 允许访问主机需要 *:// * / * li>

    • The webRequest permission is needed to enable the API.
    • The webRequestBlocking permission is needed to make the request handling synchronous, to enable the function to modify (cancel) the request.
    • The *://*/* permission is needed to allow access to the host
    • 这篇关于如何在Google Chrome扩展程序中阻止iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆