zxlie / FeHelper

😍FeHelper--Web前端助手(Awesome!Chrome & Firefox & MS-Edge Extension, All in one Toolbox!)
https://www.baidufe.com/fehelper
MIT License
5k stars 1.24k forks source link

疑问:为什么无法获取 chrome.webRequest 对象,不是在 background 运行环境吗 #136

Open Pluckypan opened 3 years ago

Pluckypan commented 3 years ago
/**
 * 注意这里的方法名称,其实是:window[`${toolName.replace(/[-_]/g,'')}ContentScript`];
 * @author 阿烈叔
 */
window.helloworldContentScript = function () {
    console.log('你好,我是来自FeHelper的工具Demo:hello world!');
};

/**
 * 如果在 fh-config.js 中指定了 noPage参数为true,则这里必须定义noPage的接口方法,如:
 * 注意这里的方法名称,其实是:window[`${toolName.replace(/[-_]/g,'')}NoPage`];
 * @author 阿烈叔
 */
window.helloworldNoPage = function (tabInfo) {
    alert('你好,我是来自FeHelper的工具Demo:hello world!你可以打开控制台看Demo的输出!');
    console.log('你好,我是来自FeHelper的工具Demo:', tabInfo);

    // background 示例:获取当前浏览器的所有tab
    chrome.runtime.sendMessage({
        type: 'fh-dynamic-any-thing',
        params: {
            tabId: window.__FH_TAB_ID__ // 这是FH的内置变量,表示当前Tab的id
        },
        func: ((params, callback) => {
            // TODO: 这里可以调用 chrome.* 的API,随便用。。。
            try{
               var x = chrome.webRequest.onBeforeRequest
               alert(x)
            }catch(e){
               alert(e)
            }
            callback && callback();
            return true;
        }).toString()
    });
};

出问题的代码如上 直接在 示例代码中加的

之前都是自己写插件的 在 background 环境下 chrome.webRequest 都是正常的 发现有 FeHelper 基本能满足大部分场景后就 没有再自己开发插件了

现在的需求是监听页面 api 请求, 请教下在 FeHelper 里该如何 实现

Pluckypan commented 3 years ago

@zxlie

Pluckypan commented 3 years ago
"permissions": [
    "tabs",
    "contextMenus",
    "cookies",
    "notifications",
    "activeTab",
    "storage",
    "unlimitedStorage",
    "<all_urls>"
  ],
  "optional_permissions": [
    "downloads",
    "webRequest",
    "webRequestBlocking"
  ],

应该需要把 "webRequest", "webRequestBlocking" 放入到 permissions