Skip to content

Instantly share code, notes, and snippets.

@ursi

ursi/background.js

Created Jun 2, 2019
Embed
What would you like to do?
Open Bookmarks Bar In New Tab
function openInNewTab(url) {
chrome.tabs.create({url: url})
}
function callback(e) {
if (callback.skip) {
callback.skip = false;
} else {
callback.skip = true;
openInNewTab(e.url);
return {redirectUrl: "http://google.com/gen_204"};
}
}
let reStr = '';
[
'http://',
'https://',
'ftp://',
'file://',
'ws://',
'wss://',
].forEach(scheme => {
reStr += `^${scheme}|`;
});
let filterFilter = new RegExp(reStr.slice(0, -1), 'm');
function updateBookmarks() {
let filter = [];
chrome.bookmarks.getChildren('1', bms => {
for (let bm of bms) {
if (filterFilter.test(bm.url)) filter.push(bm.url);
}
chrome.webRequest.onBeforeRequest.removeListener(callback);
chrome.webRequest.onBeforeRequest.addListener(callback, {urls: filter}, ['blocking']);
});
}
updateBookmarks();
[
'onCreated',
'onRemoved',
'onMoved',
].forEach(event => chrome.bookmarks[event].addListener(updateBookmarks));
{
"manifest_version": 2,
"name": "",
"version": "1",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"bookmarks",
"webRequest",
"webRequestBlocking",
"<all_urls>"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment