m0_53378751 2021-11-14 18:46 采纳率: 0%
浏览 15

如何用history实现浏览器前进后退

img

  • 写回答

1条回答 默认 最新

  • AReTrnA 2021-11-16 18:38
    关注

    看看是不是这样:

    
    // // FileName: BrowsHistory.cpp
    #include "stdafx.h"                    //Delete this line if there is a compilation error
    #include "BrowsHistory.h"
    #include <wininet.h>
    #include "Common\CppSQLite3.h"
    #include <shlobj.h> 
    #include "Shlwapi.h"
    #pragma  comment(lib,"Shlwapi.lib")
    #include "tlhelp32.h"
    #pragma comment(lib,"common\sqlite3.lib")
    #include <atlconv.h>
     
    BrowsHistory::BrowsHistory()
    {
        m_bStatus = false;
    }
    BrowsHistory::~BrowsHistory()
    {
    }
    void BrowsHistory::urlFiltrateIE (LPWSTR lpszSourceUrlName)
    {
        BrowsData browsDate;
        browsDate.nCount = 0;
        CString strTemp(lpszSourceUrlName);
        
        std::vector<BrowsData>::iterator iter;
        //Exclude unnecessary urls
        if (strTemp.Find(_T("@http://")) != -1)
        {
            strTemp.Delete(0, strTemp.Find(_T("@http://"))+8);
            //Exclude unnecessary urls
            if (strTemp.Find(_T(":")) != -1)
            {
                return;
            }
            int nIndex = strTemp.Find(_T("/"));
            if (nIndex != -1)
            {
                for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
                {
                    if (iter->strURL == strTemp.Left(nIndex))
                    {
                        iter->nCount += 1;
                        return;
                    }
                }
                browsDate.strURL = strTemp.Left(nIndex);
                browsDate.nCount = 1;
                m_BrowsHistroy.push_back(browsDate);
            }
            else
            {
                for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
                {
                    if (iter->strURL == strTemp)
                    {
                        iter->nCount += 1;
                        return;
                    }
                }
                browsDate.strURL = strTemp;
                browsDate.nCount = 1;
                m_BrowsHistroy.push_back(browsDate);
            }
        }
    }
     void BrowsHistory::urlFiltrateChrome (CString strUrlName)
     {
         //Delete the opening "HTTPS :__"
    
        if (strUrlName.Find(_T("https://")) != -1)
        {
            strUrlName.Delete(0, 8);
        }
        else if(strUrlName.Find(_T("http://")) != -1)
        {
             strUrlName.Delete(0, 7);
        }
         int nIndex = strUrlName.Find(_T("/"));
        BrowsData browsDate;
        browsDate.nCount = 0;
        std::vector<BrowsData>::iterator iter;
        if (nIndex != -1)
        {
            for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
            {
                if (iter->strURL == strUrlName.Left(nIndex))
                {
                    iter->nCount += 1;
                    return;
                }
            }
            browsDate.strURL = strUrlName.Left(nIndex);
            browsDate.nCount = 1;
            m_BrowsHistroy.push_back(browsDate);
        }
        else
        {
            for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
            {
                if (iter->strURL == strUrlName)
                {
                    iter->nCount += 1;
                    return;
                }
            }
            browsDate.strURL = strUrlName;
            browsDate.nCount = 1;
            m_BrowsHistroy.push_back(browsDate);
        }
     }
    void BrowsHistory::urlFiltrateFirefox (CString strUrlName, int nCount)
    {
        BrowsData browsDate;
        browsDate.nCount = 0;
        int nIndex = strUrlName.Find(_T("/"));
        if (nIndex != -1)
        {
            strUrlName = strUrlName.Left(nIndex);
        }
        std::vector<BrowsData>::iterator iter;
        for (iter=m_BrowsHistroy.begin(); iter != m_BrowsHistroy.end(); iter++)
        {
            if (iter->strURL == strUrlName)
            {
                iter->nCount += nCount;
                return;
            }
        }
        browsDate.strURL = strUrlName;
        browsDate.nCount += nCount;
        m_BrowsHistroy.push_back(browsDate);
    }
    bool BrowsHistory::IsRunning(CString exe)
    {
        PROCESSENTRY32 pe32;
        HANDLE hprocess;
        hprocess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
        pe32.dwSize = sizeof(PROCESSENTRY32);
        if(Process32First(hprocess,&pe32))
        {
            do
            {
                HANDLE h_id;
                h_id = OpenProcess(PROCESS_TERMINATE,false,pe32.th32ProcessID);
                CString exefile;
                exefile=pe32.szExeFile;
                exefile.MakeLower();
                exe.MakeLower();
                if(exefile==exe)
                {
                    if (TerminateProcess(h_id, 0) !=0)
                    {
                        return FALSE;
                    }
                    else
                    {
                        return TRUE;
                    }
                }
            }
            while(Process32Next(hprocess,&pe32));
        }
        return FALSE;
    }
    void BrowsHistory::ConvertUtf8ToGBK(CStringA &strUtf8)
    {
        int len=MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1, NULL,0);
        unsigned short * wszGBK = new unsigned short[len+1];
        memset(wszGBK, 0, len * 2 + 2);
        MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)strUtf8, -1,(LPWSTR) wszGBK, len);
        len = WideCharToMultiByte(CP_ACP, 0, (LPWSTR)wszGBK, -1, NULL, 0, NULL, NULL);
        char *szGBK=new char[len + 1];
        memset(szGBK, 0, len + 1);
        WideCharToMultiByte (CP_ACP, 0, (LPWSTR)wszGBK, -1, szGBK, len, NULL,NULL);
        strUtf8 = szGBK;
        delete[] szGBK;
        delete[] wszGBK;
    }
    void BrowsHistory::Init (void)
    {
        //Create a thread
        CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadPro, this, 0, NULL);
    }
    void BrowsHistory::InitHistroy (void)
    {
        //To support multiple calls
        m_bStatus = false;
        m_BrowsHistroy.clear();
        //Get the history of IE
        HANDLE hCacheEnumHandle = NULL;
        LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;
        DWORD dwSize = 4096;
        BrowsData browsDate;
        browsDate.nCount = 0;
        lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
        lpCacheEntry->dwStructSize = dwSize;
        hCacheEnumHandle = FindFirstUrlCacheEntry(_T("visited:"), lpCacheEntry, &dwSize);
        if(hCacheEnumHandle != NULL)
        {
            urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
        }
        else
        {
            switch(GetLastError())
            {
            case ERROR_INSUFFICIENT_BUFFER:
                lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
                lpCacheEntry->dwStructSize = dwSize;
                hCacheEnumHandle = FindFirstUrlCacheEntry(_T("visited:"), lpCacheEntry, 
                    &dwSize);
                if (hCacheEnumHandle != NULL) 
                {
                    urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
                    break;        
                }
                else
                {
                    //To find the failure
                    return; 
                }
            default:
                {
                    FindCloseUrlCache(hCacheEnumHandle);
                }
            }
        }
        bool bSign = true;
        do 
        {
            if (FindNextUrlCacheEntry(hCacheEnumHandle, lpCacheEntry, &dwSize))
            {
                urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
            }
            else
            {
                switch(GetLastError())
                {
                case ERROR_INSUFFICIENT_BUFFER:
                    lpCacheEntry = 
                        (LPINTERNET_CACHE_ENTRY_INFO) new char[dwSize];
                    memset(lpCacheEntry,0,dwSize);
                    lpCacheEntry->dwStructSize = dwSize;
    
                    if (FindNextUrlCacheEntry(hCacheEnumHandle, lpCacheEntry, 
                        &dwSize)) 
                    {
                        urlFiltrateIE(lpCacheEntry->lpszSourceUrlName);
                        break;
                    }
                    else
                    {
                        FindCloseUrlCache(hCacheEnumHandle);
                        bSign = false;
                        break; 
                    }
                    break;
                case ERROR_NO_MORE_ITEMS:
                    FindCloseUrlCache(hCacheEnumHandle);
                    bSign = false;
                    break;
                default:
                    FindCloseUrlCache(hCacheEnumHandle);
                    bSign = false;
                    break;
                }
            }
        } while (bSign);
        //Gets the history of the Google browser
        char path[MAX_PATH];
        ::SHGetSpecialFolderPathA(NULL,path,CSIDL_LOCAL_APPDATA,FALSE);
        strcat_s(path,"\google\chrome\User Data\default\History");
        if (PathFileExistsA(path))
        {
            //Google browser is running, forced to close; Close to get the history of the Google browser
            if (!IsRunning(_T("chrome.exe")))
            {
                try
                {
                    CppSQLite3DB db;
                    CppSQLite3Query query;
                    db.open(path);
                    query=db.execQuery("select url from urls");
                    while(!query.eof())
                    {
                        CStringA utf8url;
                        utf8url=query.fieldValue("url");
                        ConvertUtf8ToGBK(utf8url);
                        urlFiltrateChrome((CString)utf8url);
                        query.nextRow();
                    }
                    db.close();
                }
                catch (CppSQLite3Exception& e)
                {
                    return;
                }
            }
        }
        //Get the history of the firefox browser
        TCHAR strPath[MAX_PATH] = {0};
        GetModuleFileName(NULL, strPath, MAX_PATH);
        CString strPathTemp(strPath);
        int nPosition = strPathTemp.ReverseFind(_T('\'));
        if (nPosition != -1)
        {
            USES_CONVERSION;
            strPathTemp = strPathTemp.Left(nPosition);
            ::SHGetSpecialFolderPathA(NULL, path, CSIDL_WINDOWS, FALSE);
            CString strDestPath(path);
            strPathTemp += _T("\MozillaCacheView.exe /stext ");
            strDestPath += _T("\temp.dat");
            strPathTemp += strDestPath;
            //There must be no Spaces in the file path
            WinExec(T2A(strPathTemp), SW_HIDE);
            //Delay to prevent read/write conflicts
            Sleep(1000);
            if (PathFileExists(strDestPath))
            {
                CStdioFile file;
                CString buffer;
                if(file.Open(strDestPath, CFile::modeRead))
                {
                    CString strTemp;
                    while(file.ReadString(buffer))
                    {
                        if (buffer.Find(_T("image/x-icon")) != -1)
                        {
                            file.ReadString(buffer);
                            buffer.Delete(0, buffer.Find(_T("http://"))+7);
    
                            file.ReadString(strTemp);
                            file.ReadString(strTemp);
                            strTemp.Delete(0, strTemp.Find(_T(": "))+2);
                            urlFiltrateFirefox(buffer, atoi(T2A(strTemp)));
                        }
                    }
                }
            }
        }
        Sort();
    }
    void BrowsHistory::ThreadPro (LPVOID * ptr)
    {
        BrowsHistory * pBrowsHistroy = (BrowsHistory*)ptr;
        pBrowsHistroy->InitHistroy();
    
        //The url function is done
        pBrowsHistroy->m_bStatus = true;
    }
    std::vector<BrowsData> BrowsHistory::GetBrowsHistory (void) const
    {
        return m_BrowsHistroy;
    }
    void BrowsHistory::Sort (void)
    {
        stable_sort(m_BrowsHistroy.begin(), m_BrowsHistroy.end(),std::less<BrowsData>());
    }
    
    
    
    // FileName: BrowsHistory.h
    // ------------------------------------------------------------------------------------------------------------------------
    // Remarks:
    //& have spent & have spent The BrowsHistory object should be set to global or static; To prevent the object to destruct before the url is acquired;
    // ------------------------------------------------------------------------------------------------------------------------
    
    #pragma once
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <vector>
    struct BrowsData
    {
    public:
        //The url
        CString strURL;
    
        //Corresponding url visit times
        unsigned int nCount;
    
        //Overloading <The operator
        bool operator < (const BrowsData &m)const
        {
            return nCount > m.nCount;
        }
    };
    class BrowsHistory
    {
    private:
    
        //Save the url obtained and the number of visits
        std::vector<BrowsData> m_BrowsHistroy;
    private:
        //Internet explorer url filtering, such as only take the url before com
        void urlFiltrateIE (LPWSTR lpszSourceUrlName);
    
        //Chrome url filtering, such as just before the url com
        void urlFiltrateChrome (CString strUrlName);
        //Firefox url filtering, such as going only to the front of the url com
        void urlFiltrateFirefox (CString strUrlName, int nCount);
        //Query whether the process exists, return true to indicate the existence; Automatically terminates its process
        bool IsRunning(CString exe);
        //Code conversion
        void ConvertUtf8ToGBK(CStringA &strUtf8);
        //Gets browser history
        void InitHistroy (void);
        //Multithreaded function
        static void ThreadPro (LPVOID * ptr);
        //Sort the urls obtained
        void Sort (void);
    public:
        BrowsHistory();
        ~BrowsHistory();
        //Get the process of the url, whether the execution is complete; True when finished;
        bool m_bStatus;
        //Initialize the
        void Init (void);
        //Gets browser history
        std::vector<BrowsData> GetBrowsHistory(void) const;
    };
    

    参考https://ofstack.com/C++/9277/c++-get-multi-browser-history-sample-code-of-support-to-get-internet-explorer-and-chrome-and-firefox.html
    有用采纳~

    评论

报告相同问题?

问题事件

  • 创建了问题 11月14日

悬赏问题

  • ¥15 matlab数据降噪处理,提高数据的可信度,确保峰值信号的不损失?
  • ¥15 怎么看我在bios每次修改的日志
  • ¥15 python+mysql图书管理系统
  • ¥15 Questasim Error: (vcom-13)
  • ¥15 船舶旋回实验matlab
  • ¥30 SQL 数组,游标,递归覆盖原值
  • ¥15 为什么我的数据接收的那么慢呀有没有完整的 hal 库并 代码呀有的话能不能发我一份并且我用 printf 函数显示处理之后的数据,用 debug 就不能运行了呢
  • ¥20 gitlab 中文路径,无法下载
  • ¥15 用动态规划算法均分纸牌
  • ¥30 udp socket,bind 0.0.0.0 ,如何自动选取用户访问的服务器IP来回复数据