-
-
Save Raidus/1f22ac8f75fdf42e5db4e11d627ef28c to your computer and use it in GitHub Desktop.
Puppeteer (v.0.12.0) navigation blocking workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
// keep track of all the opened tab | |
let tabs = {}; | |
// Get all existing tabs | |
chrome.tabs.query({}, function(results) { | |
results.forEach(function(tab) { | |
tabs[tab.id] = tab; | |
}); | |
}); | |
// Create tab event listeners | |
function onUpdatedListener(tabId, changeInfo, tab) { | |
tabs[tab.id] = tab; | |
} | |
function onRemovedListener(tabId) { | |
delete tabs[tabId]; | |
} | |
/** | |
* if the request url differ from the current tab url block it | |
* @param details | |
* @return {{redirectUrl: string}} | |
*/ | |
function onBeforeRequestListener(details) { | |
let currentTab = tabs[details.tabId]; | |
if (currentTab.url.startsWith('http') && _compareUrls(details.url, currentTab.url)) { | |
console.warn(`Navigation to ${details.url} blocked.`); | |
chrome.tabs.executeScript(details.tabId, {file: 'content.js'}, function() { | |
chrome.tabs.sendMessage(details.tabId, {url: details.url}); | |
}); | |
return {redirectUrl: 'javascript:void(0)'}; | |
} | |
} | |
// Subscribe to tab events to track opened tabs | |
chrome.tabs.onUpdated.addListener(onUpdatedListener); | |
chrome.tabs.onRemoved.addListener(onRemovedListener); | |
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequestListener, { | |
urls: ['<all_urls>'], | |
types: ['main_frame', 'sub_frame'], // only watching for "frame" type request | |
}, ['blocking']); | |
/** | |
* compare 2 urls based on there href form WITHOUT the hash part | |
* @param {string} url1 | |
* @param {string} url2 | |
* @return {boolean} | |
* @private | |
*/ | |
function _compareUrls(url1, url2) { | |
let cleanedUrl1 = new URL(url1), | |
cleanedUrl2 = new URL(url2); | |
cleanedUrl1.hash = ''; | |
cleanedUrl2.hash = ''; | |
return cleanedUrl1.href !== cleanedUrl2.href; | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
// transmitting url received from the background page to the page | |
chrome.runtime.onMessage.addListener(function(msg) { | |
window.postMessage({type: 'NavigationBlocked', url: msg.url}, '*'); | |
}); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
const puppeteer = require('puppeteer'); | |
const browser = await puppeteer.launch({ | |
headless: false, | |
args: { | |
'--load-extension=/path/to/chrome_extension/', | |
'--disable-extensions-except=path/to/chrome_extension/', | |
} | |
}); | |
const page = await browser.newPage(); | |
await page.goto('https://mypage.lan/example_navigation_away_test_case.html'); | |
await page.click('a'); | |
console.log(await page.content()); | |
await page.screenshot({path: 'screenshot.png'}); | |
await browser.close(); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"manifest_version": 2, | |
"name": "Puppeteer workaround - Navigation blocker", | |
"version": "1.0", | |
"permissions": [ | |
"webRequest", | |
"*://*/*", | |
"webRequestBlocking", | |
"tabs" | |
], | |
"background": { | |
"persistent": true, | |
"scripts": [ | |
"background.js" | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment