Skip to content

Commit

Permalink
Fixed #2
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaeleflorio committed Nov 29, 2017
1 parent 13b16d5 commit 056c7ae
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
47 changes: 47 additions & 0 deletions chrome/content_scripts/aHandler.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
})();
9 changes: 9 additions & 0 deletions chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@
]
},

"content_scripts": [{
"js": [
// "webextension-browser-proxy/polyfill.js",
"content_scripts/aHandler.js"
],
"matches": ["<all_urls>"],
"run_at": "document_end"
}],

"permissions": [
"contextMenus",
"storage",
Expand Down
10 changes: 9 additions & 1 deletion common/backgrounds/qur.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
{
Expand All @@ -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;

})();

12 changes: 7 additions & 5 deletions common/backgrounds/whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down

0 comments on commit 056c7ae

Please sign in to comment.