【问题标题】:How to get third-party domains LocalStorage in Puppeter?如何在 Puppeteer 中获取第三方域名 LocalStorage?
【发布时间】:2022-12-13 09:08:54
【问题描述】:

我正在使用 PHP 包装器puphpeteer,但最后是一样的。

我正在尝试获取 Youtube iframe 在特定站点上投放的 localSorage 变量。但是,当我尝试通过从 contentWindow 访问它来获取它的 localStorage 时,出现跨源错误:

Evaluation failed: DOMException: Blocked a frame with origin "https://www.fundacionhortensiaherrero.org" from accessing a cross-origin frame.
    at __puppeteer_evaluation_script__:3:46 {"userId":3,"exception":"[object] (Nesk\\Rialto\\Exceptions\\Node\\FatalException(code: 0): Evaluation failed: DOMException: Blocked a frame with origin \"https://www.fundacionhortensiaherrero.org\" from accessing a cross-origin frame.

编码:

$youtube_iframes = $this->page->querySelectorAll('iframe[src*="youtube.com"]');
foreach ($youtube_iframes as $iframe){
    $iframe->evaluate(JsFunction::createWithParameters(['el'])->body("el.setAttribute('src',el.getAttribute('src')+'&autoplay=1')"));
    sleep(2);
    Log::info($iframe->evaluate(JsFunction::createWithParameters(['el'])->body("return { ...el.contentWindow.localStorage }")));
}

如果我return { ...localStorage },我得到网站的localStorage,但不是 Youtube 的。

一定可以这样做吗?最后它是“我的浏览器”,我应该能够访问任何域的 localStorage。

你可以在这个 URL 上测试它:https://www.fundacionhortensiaherrero.org/valencia-ya-disfruta-la-exposicion-manolo-valdes/

【问题讨论】:

    标签: puppeteer puphpeteer


    【解决方案1】:

    建议的方法

    您可以在 Puppeteer 导航的所有主机上构建 localStorage 的“运行总数”。

    此答案基于 Javascript,但您应该能够在 puphpeteer...

    获取所有localStorage的代码

    // Object to store all of the localStorage.
    let runningLocalStorage = {}
    
    // This will be called from within Puppeteer.
    const updateRunningLocalStorage = (host, localStorage) => {
        runningLocalStorage[host] = localStorage
    }
    
    // Expose a Node.js function for the browser to call.
    await page.exposeFunction("updateLocalStorage", updateRunningLocalStorage)
    
    // Attach event to 'beforeunload', so that we can capture the localStorage before the browser navigates away.
    await page.evaluateOnNewDocument(() => {
        window.addEventListener("beforeunload", function (e) {
            updateRunningLocalStorage(window.location.host, window.localStorage)
        })
    })
    
    // Do the rest of your normal Puppeteer logic here...... 
    
    // Finally get the final localStorage (because 'beforeunload' event won't get fired at the end)get fired at the end)
    const finalLocalStorage = await page.evaluate(() => {
        return {
            host: window.location.host,
            localStorage: Object.assign({}, window.localStorage)
        }
    })
    
    updateRunningLocalStorage(finalLocalStorage.host, finalLocalStorage.localStorage)
    
    // runningLocalStorage should now have all of our localStorage across all domains.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2020-06-05
      • 2018-03-13
      • 2017-06-28
      • 2019-06-18
      • 2010-10-16
      • 1970-01-01
      相关资源
      最近更新 更多