Skip to content

Instantly share code, notes, and snippets.

@ursi
Created June 2, 2019 06:24
Show Gist options
  • Select an option

  • Save ursi/03893a3d017e7104fabed36e856c7ddf to your computer and use it in GitHub Desktop.

Select an option

Save ursi/03893a3d017e7104fabed36e856c7ddf to your computer and use it in GitHub Desktop.

Revisions

  1. ursi created this gist Jun 2, 2019.
    45 changes: 45 additions & 0 deletions background.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    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));
    14 changes: 14 additions & 0 deletions manifest.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    {
    "manifest_version": 2,
    "name": "",
    "version": "1",
    "background": {
    "scripts": ["background.js"]
    },
    "permissions": [
    "bookmarks",
    "webRequest",
    "webRequestBlocking",
    "<all_urls>"
    ]
    }