diff --git a/chrome/content_scripts/aHandler.js b/chrome/content_scripts/aHandler.js new file mode 100644 index 0000000..7ebea23 --- /dev/null +++ b/chrome/content_scripts/aHandler.js @@ -0,0 +1,47 @@ +/* TODO: + * FASTER check whitelisted URL + */ + +(() => { + const aHandler = (node) => { + const config = {subtree: false, childList: false, attributes: true}; + + const setTarget = (node) => { + if (node.href.indexOf(window.location.href) !== 0) + node.target = "_blank"; + + node.target = "_blank"; + + /* disabled for performance reason */ + // browser.runtime.sendMessage({msg: "isWhitelisted", url: node.href}) + // .then((response) => { + // if (response.result === false) + // node.target = "_blank"; + // }) + // .catch (() => node.target = "_blank"); + }; + + const mutHandler = (mutations, observer) => { + const a = mutations[0].target; + observer.disconnect(); + setTarget(a); + observer.observe(a, config); + }; + + setTarget(node); + (new MutationObserver(mutHandler)).observe(node, config); + }; + + const anchors = Array.from(document.getElementsByTagName("a")); + anchors.forEach((a) => aHandler(a)); + + const documentObserver = new MutationObserver((mutations) => { + mutations.forEach((m) => { + const nodes = Array.from(m.addedNodes); + nodes.forEach((n) => { + if (n.nodeName === "A") + aHandler(n); + }); + }); + }); +})(); diff --git a/chrome/manifest.json b/chrome/manifest.json index 9a15a5e..a613941 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -36,6 +36,15 @@ ] }, + "content_scripts": [{ + "js": [ +// "webextension-browser-proxy/polyfill.js", + "content_scripts/aHandler.js" + ], + "matches": [""], + "run_at": "document_end" + }], + "permissions": [ "contextMenus", "storage", diff --git a/common/backgrounds/qur.js b/common/backgrounds/qur.js index 0f558cb..be128dc 100644 --- a/common/backgrounds/qur.js +++ b/common/backgrounds/qur.js @@ -4,11 +4,13 @@ const qur = { }; /* init script */ (async () => { + /* pupulate qur namespace */ qur.whitelist = await whitelist(); qur.settings = await settings(); qur.native = native; qur.anti_rdr = anti_rdr(); + /* listeners */ browser.webRequest.onBeforeRequest.addListener( redirector().route, { @@ -18,10 +20,16 @@ const qur = { }; ["blocking"] ); + /* disabled because used only by chrome/content_scripts/aHandler.js */ + // browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + // if (request.msg === "isWhitelisted") + // sendResponse({result: qur.whitelist.test(anti_rdr.escape(request.url), false)}); + // }); + + /* ui stuffs */ ui.init(); /* callable by getBackgroundPage() */ getQur = () => qur; })(); - diff --git a/common/backgrounds/whitelist.js b/common/backgrounds/whitelist.js index fff69f0..ddf4494 100644 --- a/common/backgrounds/whitelist.js +++ b/common/backgrounds/whitelist.js @@ -67,11 +67,13 @@ const whitelist = async () => { } }, - test: (regex) => { - const tmpIndex = getIndex(regex, tmpBuffer); - if (tmpIndex !== -1) { - tmpBuffer.splice(tmpIndex, 1); - return true; + test: (regex, checkTmp = true) => { + if (checkTmp === true) { + const tmpIndex = getIndex(regex, tmpBuffer); + if (tmpIndex !== -1) { + tmpBuffer.splice(tmpIndex, 1); + return true; + } } return buffer.some((e) => {