首页 > 解决方案 > 我的 react 应用程序中有一个 google chrome 扩展程序。我需要知道文档的哪一部分可以帮助我

问题描述

所以基本上我有一个 chrome 扩展程序创建反应应用程序。我想制作一个功能,如果我点击一个特定的站点并将其重定向到一个选项卡然后 - 如果扩展程序当前已添加到浏览器。我需要显示一个当我们在单击特定按钮时重定向模式。

我不知道如何使用 Chrome 扩展 API 来实现它。这是我添加和删除扩展的代码。

    if (details.reason === "install") {
        Background.initStorage(null).then(({ extId }) => {
            // Should be called on install of extension
            Background.fetchUserCountry();
            // Opens thank-you in current tab
            chrome.tabs.query({ currentWindow: true, active: true }, tabs => {
                let url = new URL(tabs[0].url);
                let refString = "";

                for (let x = 1; x <= 6; x++) {
                    if (url.searchParams.get(`ref${x}`)) {
                        refString += `&ref${x}=${url.searchParams.get(`ref${x}`)}`;
                    }
                }

                chrome.tabs.create({ url: Config.wwwServer + '/api/install?app_uid=' + extId + '&v=' + Config.version + '&token=' + Config.apiToken + '&r=thankyou' + refString }, tab => {
                    const listener = function (tabId) {
                        if (tabId === tab.id) {
                            chrome.tabs.get(tabId, tabResult => {
                                if (tabResult) {
                                    chrome.tabs.executeScript(tabId,
                                        { code: "var toolbar = document.createElement('TOOLBAR'); toolbar.id = 'browserappinstalled'; toolbar.setAttribute('app_uid', '" + extId + "'); toolbar.setAttribute('version', '" + Config.version + "'); document.body.appendChild(toolbar);" },
                                        () => console.log("Extension installed", extId)
                                    );
                                }
                            });
                        }
                        chrome.tabs.onUpdated.removeListener(listener);
                    };
                    chrome.tabs.onUpdated.addListener(listener);
                });
            });
            chrome.runtime.setUninstallURL(Config.apiServer + '/ext/uninstall?app_uid=' + extId + '&v=' + Config.version);

            chrome.tabs.query({ 'url': Config.lpUrls }, tabs => {
                tabs.forEach(tab => chrome.tabs.remove(tab.id))
            });
        });
    } else if (details.reason === 'update') {
        // Should be called on update of extension
        Background.fetchUserCountry();

        Background.initStorage(null);
    }
});

标签: javascriptreactjsgoogle-chromegoogle-chrome-extension

解决方案


推荐阅读