| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This Source Code is subject to the terms of the Mozilla Public License | 2 * This Source Code is subject to the terms of the Mozilla Public License |
| 3 * version 2.0 (the "License"). You can obtain a copy of the License at | 3 * version 2.0 (the "License"). You can obtain a copy of the License at |
| 4 * http://mozilla.org/MPL/2.0/. | 4 * http://mozilla.org/MPL/2.0/. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["http://* /*", "https://*/*"]}, ["blocking"]); | 7 chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["http://* /*", "https://*/*"]}, ["blocking"]); |
| 8 chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: [" http://*/*", "https://*/*"]}, ["requestHeaders", "blocking"]); | 8 chrome.webRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, {urls: [" http://*/*", "https://*/*"]}, ["requestHeaders", "blocking"]); |
| 9 chrome.tabs.onRemoved.addListener(forgetTab); | 9 chrome.tabs.onRemoved.addListener(forgetTab); |
| 10 | 10 |
| 11 var frames = {}; | 11 var frames = {}; |
| 12 var tabs = {}; | |
| 13 | 12 |
| 14 function onBeforeRequest(details) | 13 function onBeforeRequest(details) |
| 15 { | 14 { |
| 15 if (details.tabId == -1) | |
| 16 return {}; | |
| 17 | |
| 16 var type = details.type; | 18 var type = details.type; |
| 17 if (type == "main_frame" || type == "sub_frame") | 19 if (type == "main_frame" || type == "sub_frame") |
| 18 recordFrame(details.tabId, details.frameId, details.url, type == "main_frame "); | 20 recordFrame(details.tabId, details.frameId, details.parentFrameId, details.u rl, type == "main_frame"); |
|
Felix Dahlke
2012/09/18 13:23:14
type == "main_frame" is redundant here, I'd assign
| |
| 19 | 21 |
| 20 if (type == "main_frame") | 22 if (type == "main_frame") |
| 21 return; | 23 return {}; |
| 22 | 24 |
| 23 // Type names match Mozilla's with main_frame and sub_frame being the only exc eptions. | 25 // Type names match Mozilla's with main_frame and sub_frame being the only exc eptions. |
| 24 if (type == "sub_frame") | 26 if (type == "sub_frame") |
| 25 type = "SUBDOCUMENT"; | 27 type = "SUBDOCUMENT"; |
| 26 else | 28 else |
| 27 type = type.toUpperCase(); | 29 type = type.toUpperCase(); |
| 28 | 30 |
| 29 var documentUrl = getFrameUrl(details.tabId, details.frameId); | 31 var frame = (type != "SUBDOCUMENT" ? details.frameId : details.parentFrameId); |
| 30 var topUrl = getTabUrl(details.tabId); | 32 var filter = checkRequest(type, details.tabId, details.url, frame); |
| 31 if (type == "SUBDOCUMENT") | |
| 32 documentUrl = getFrameUrl(details.tabId, details.parentFrameId) || topUrl; | |
| 33 | |
| 34 var filter = checkRequest(type, details.url, documentUrl, topUrl); | |
| 35 if (filter instanceof BlockingFilter) | 33 if (filter instanceof BlockingFilter) |
| 36 { | 34 { |
| 37 var collapse = filter.collapse; | 35 var collapse = filter.collapse; |
| 38 if (collapse == null) | 36 if (collapse == null) |
| 39 collapse = (localStorage["hidePlaceholders"] != "false"); | 37 collapse = (localStorage["hidePlaceholders"] != "false"); |
| 40 if (collapse && type == "SUBDOCUMENT") | 38 if (collapse && type == "SUBDOCUMENT") |
| 41 return {redirectUrl: "about:blank"}; | 39 return {redirectUrl: "about:blank"}; |
| 42 else if (collapse && type == "IMAGE") | 40 else if (collapse && type == "IMAGE") |
| 43 return {redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAA ABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="}; | 41 return {redirectUrl: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAA ABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg=="}; |
| 44 else | 42 else |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 56 var headers = details.requestHeaders || []; | 54 var headers = details.requestHeaders || []; |
| 57 if (!headers.some(function(header) { header.name == "DNT";})) | 55 if (!headers.some(function(header) { header.name == "DNT";})) |
| 58 { | 56 { |
| 59 headers.push({name: "DNT", value: "1"}); | 57 headers.push({name: "DNT", value: "1"}); |
| 60 return {requestHeaders: headers}; | 58 return {requestHeaders: headers}; |
| 61 } | 59 } |
| 62 } | 60 } |
| 63 return null; | 61 return null; |
| 64 } | 62 } |
| 65 | 63 |
| 66 function recordFrame(tabId, frameId, frameUrl, isMain) | 64 function recordFrame(tabId, frameId, parentFrameId, frameUrl, isMain) |
|
Felix Dahlke
2012/09/18 13:23:14
This parameter is no longer used, it could be remo
| |
| 67 { | 65 { |
| 68 if (!(tabId in frames)) | 66 if (!(tabId in frames)) |
| 69 frames[tabId] = {}; | 67 frames[tabId] = {}; |
| 70 frames[tabId][frameId] = frameUrl; | 68 frames[tabId][frameId] = {url: frameUrl, parent: parentFrameId}; |
| 71 | |
| 72 if (isMain) | |
| 73 tabs[tabId] = frameUrl; | |
| 74 } | 69 } |
| 75 | 70 |
| 76 function getFrameUrl(tabId, frameId) | 71 function getFrameUrl(tabId, frameId) |
| 77 { | 72 { |
| 78 if (tabId in frames && frameId in frames[tabId]) | 73 if (tabId in frames && frameId in frames[tabId]) |
| 79 return frames[tabId][frameId]; | 74 return frames[tabId][frameId].url; |
| 80 return null; | 75 return null; |
| 81 } | 76 } |
| 82 | 77 |
| 83 function getTabUrl(tabId) | 78 function getFrameParent(tabId, frameId) |
| 84 { | 79 { |
| 85 if (tabId in tabs) | 80 if (tabId in frames && frameId in frames[tabId]) |
| 86 return tabs[tabId]; | 81 return frames[tabId][frameId].parent; |
| 87 return null; | 82 return -1; |
| 88 } | 83 } |
| 89 | 84 |
| 90 function forgetTab(tabId) | 85 function forgetTab(tabId) |
| 91 { | 86 { |
| 92 delete frames[tabId]; | 87 delete frames[tabId]; |
| 93 delete tabs[tabId]; | |
| 94 } | 88 } |
| 95 | 89 |
| 96 function checkRequest(type, url, documentUrl, topUrl) | 90 function checkRequest(type, tabId, url, frameId) |
| 97 { | 91 { |
| 98 if (topUrl && isWhitelisted(topUrl)) | 92 if (isFrameWhitelisted(tabId, frameId)) |
| 99 return false; | 93 return false; |
| 100 | 94 |
| 95 var documentUrl = getFrameUrl(tabId, frameId); | |
| 101 if (!documentUrl) | 96 if (!documentUrl) |
| 102 documentUrl = topUrl; | 97 return false; |
| 103 | 98 |
| 104 var requestHost = extractHostFromURL(url); | 99 var requestHost = extractHostFromURL(url); |
| 105 var documentHost = extractHostFromURL(documentUrl); | 100 var documentHost = extractHostFromURL(documentUrl); |
| 106 var thirdParty = isThirdParty(requestHost, documentHost); | 101 var thirdParty = isThirdParty(requestHost, documentHost); |
| 107 return defaultMatcher.matchesAny(url, type, documentHost, thirdParty); | 102 return defaultMatcher.matchesAny(url, type, documentHost, thirdParty); |
| 108 } | 103 } |
| 104 | |
| 105 function isFrameWhitelisted(tabId, frameId, type) | |
| 106 { | |
| 107 var parent = frameId; | |
| 108 while (parent != -1) | |
| 109 { | |
| 110 var parentUrl = getFrameUrl(tabId, parent); | |
| 111 if (parentUrl && isWhitelisted(parentUrl, type)) | |
| 112 return true; | |
| 113 parent = getFrameParent(tabId, parent); | |
| 114 } | |
| 115 return false; | |
| 116 } | |
| OLD | NEW |