Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate ShareMeNot functionality #196

Merged
merged 14 commits into from
Jun 10, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Replacement and unblocking seem to work correctly in most cases.
  • Loading branch information
froeschele committed Jun 2, 2014
commit 86775a655dfa1875b9e1d8a7df38fd907899ff70
4 changes: 3 additions & 1 deletion lib/basedomain.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ function extractHostFromURL(/**String*/ url)
}
catch (e)
{
// Keep the empty string for invalid URIs.
// (Used to be: Keep the empty string for invalid URIs.)
// Actually, return the url as is.
host = url;
}

extractHostFromURL._lastURL = url;
Expand Down
6 changes: 3 additions & 3 deletions skin/socialwidgets.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

.sharemenotReplacementButton {
.privacyBadgerReplacementButton {
border: none !important;
cursor: pointer !important;
height: auto !important;
width: auto !important;
}

.sharemenotOriginalButton {
.privacyBadgerOriginalButton {
border: none !important;
height: 1.5em !important;
}
}
3 changes: 2 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
frameId = getFrameId(tabId, request.documentUrl);
}

if (isFrameWhitelisted(tabId, frameId, "DOCUMENT"))
if (isFrameWhitelisted(tabId, frameId, "DOCUMENT")
|| isSocialWidgetTemporaryUnblock(tabId, request.url, frameId))
{
sendResponse(false);
break;
Expand Down
94 changes: 42 additions & 52 deletions src/socialwidgets.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
/**
* Contains Chrome-specific code for the content script.
*/

var browserAbstractionLayer = (function() {
var exports = {};

/**
* Gets data about which tracker buttons need to be replaced from the main
* extension and passes it to the provided callback function.
*
* @param {Function} callback the function to call when the tracker data is
* received; the arguments passed are the folder
* containing the content script, the tracker
* data, and a mapping of tracker names to
* whether those tracker buttons need to be
* replaced
*/
exports.getTrackerData = function(callback) {
chrome.runtime.sendMessage({checkReplaceButton:document.location}, function(response) {
var trackers = response.trackers;
var trackerButtonsToReplace = response.trackerButtonsToReplace;
callback(trackers, trackerButtonsToReplace);
});
}

/**
* Unblocks the tracker with the given name from the page. Calls the
* provided callback function after the tracker has been unblocked.
*
* @param {String} trackerName the name of the tracker to unblock
* @param {Function} callback the function to call after the tracker has
* been unblocked
*/
exports.unblockTracker = function(trackerName, callback) {
var request = {
title: "unblockTracker",
"trackerName": trackerName
};
chrome.extension.sendRequest(request, callback);
}

return exports;
}());

/**
* The absolute path to the replacement buttons folder.
*/
Expand All @@ -57,7 +12,7 @@ var CONTENT_SCRIPT_STYLESHEET_PATH = chrome.extension.getURL("skin/socialwidgets
* Initializes the content script.
*/
function initialize() {
browserAbstractionLayer.getTrackerData(function (trackers, trackerButtonsToReplace) {
getTrackerData(function (trackers, trackerButtonsToReplace) {
// add the Content.css stylesheet to the page
var head = document.querySelector("head");
var stylesheetLinkElement = getStylesheetLinkElement(CONTENT_SCRIPT_STYLESHEET_PATH);
Expand All @@ -84,7 +39,7 @@ function createReplacementButtonImage(tracker) {
var details = buttonData.details;

button.setAttribute("src", buttonUrl);
button.setAttribute("class", "privacybadgerReplacementButton");
button.setAttribute("class", "privacyBadgerReplacementButton");
button.setAttribute("title", "PrivacyBadger has replaced this " + tracker.name
+ " button.");

Expand All @@ -106,7 +61,7 @@ function createReplacementButtonImage(tracker) {
// once when the user clicks on a replacement button
// (it executes for the buttons that have been previously
// clicked as well)
replaceButtonWithIframeAndUnblockTracker(button, tracker.name, iframeUrl);
replaceButtonWithIframeAndUnblockTracker(button, details, iframeUrl);
});
break;

Expand All @@ -117,7 +72,7 @@ function createReplacementButtonImage(tracker) {
// once when the user clicks on a replacement button
// (it executes for the buttons that have been previously
// clicked as well)
replaceButtonWithHtmlCodeAndUnblockTracker(button, tracker.name, details);
replaceButtonWithHtmlCodeAndUnblockTracker(button, details, details);
});
break;

Expand Down Expand Up @@ -170,15 +125,15 @@ function getStylesheetLinkElement(url) {
* @param {String} iframeUrl the URL of the iframe to replace the button
*/
function replaceButtonWithIframeAndUnblockTracker(button, tracker, iframeUrl) {
browserAbstractionLayer.unblockTracker(tracker, function() {
unblockTracker(tracker, function() {
// check is needed as for an unknown reason this callback function is
// executed for buttons that have already been removed; we are trying
// to prevent replacing an already removed button
if (button.parentNode !== null) {
var iframe = document.createElement("iframe");

iframe.setAttribute("src", iframeUrl);
iframe.setAttribute("class", "privacybadgerOriginalButton");
iframe.setAttribute("class", "privacyBadgerOriginalButton");

button.parentNode.replaceChild(iframe, button);
}
Expand All @@ -195,7 +150,7 @@ function replaceButtonWithIframeAndUnblockTracker(button, tracker, iframeUrl) {
* @param {String} html the HTML code that should replace the button
*/
function replaceButtonWithHtmlCodeAndUnblockTracker(button, tracker, html) {
browserAbstractionLayer.unblockTracker(tracker, function() {
unblockTracker(tracker, function() {
// check is needed as for an unknown reason this callback function is
// executed for buttons that have already been removed; we are trying
// to prevent replacing an already removed button
Expand Down Expand Up @@ -242,4 +197,39 @@ function replaceTrackerButtonsHelper(trackers, trackerButtonsToReplace) {
});
}

/**
* Gets data about which tracker buttons need to be replaced from the main
* extension and passes it to the provided callback function.
*
* @param {Function} callback the function to call when the tracker data is
* received; the arguments passed are the folder
* containing the content script, the tracker
* data, and a mapping of tracker names to
* whether those tracker buttons need to be
* replaced
*/
function getTrackerData(callback) {
chrome.runtime.sendMessage({checkReplaceButton:document.location}, function(response) {
var trackers = response.trackers;
var trackerButtonsToReplace = response.trackerButtonsToReplace;
callback(trackers, trackerButtonsToReplace);
});
}

/**
* Unblocks the tracker with the given name from the page. Calls the
* provided callback function after the tracker has been unblocked.
*
* @param {String} trackerName the name of the tracker to unblock
* @param {Function} callback the function to call after the tracker has
* been unblocked
*/
function unblockTracker(buttonUrl, callback) {
var request = {
"unblockSocialWidget" : true,
"buttonUrl": buttonUrl
};
chrome.runtime.sendMessage(request, callback);
}

initialize();
4 changes: 2 additions & 2 deletions src/socialwidgets.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
".DiggThisButton"
],
"replacementButton": {
"details": "http://www.digg.com/submit?url=*",
"details": "http://www.digg.com/submit?url=",
"imagePath": "Digg.svg",
"type": 0
}
Expand Down Expand Up @@ -55,7 +55,7 @@
},

"Google+": {
"domain": "google.com",
"domain": "apis.google.com",
"matchPatterns": [
"*://*.apis.google.com/js/plusone.js*",
"*://*.plusone.google.com/_/+1/fastbutton*",
Expand Down
41 changes: 40 additions & 1 deletion src/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ var importantNotifications = {
'subscription.updated': true,
'load': true
};
var temporarySocialWidgetUnblock = {};

/* Event Listeners */
chrome.webRequest.onBeforeRequest.addListener(onBeforeRequest, {urls: ["http://*/*", "https://*/*"]}, ["blocking"]);
Expand Down Expand Up @@ -291,6 +292,7 @@ function forgetTab(tabId) {
console.log('forgetting tab', tabId);
activeMatchers.removeTab(tabId)
delete frames[tabId];
delete temporarySocialWidgetUnblock[tabId];
}

function checkAction(tabId, url){
Expand All @@ -315,6 +317,10 @@ function checkAction(tabId, url){
}

function checkRequest(type, tabId, url, frameId) {
if (isSocialWidgetTemporaryUnblock(tabId, url, frameId)) {
return false;
}

if (isFrameWhitelisted(tabId, frameId)){
return false;
}
Expand Down Expand Up @@ -375,7 +381,7 @@ function checkRequest(type, tabId, url, frameId) {
//console.log("Subscription data for " + requestHost + " is: " + JSON.stringify(blockedData[requestHost]));
var action = activeMatchers.getAction(tabId, requestHost);
if (action && action != 'noaction'){
//console.log("Action to be taken for " + requestHost + ": " + action);
//console.log("Action to be taken for " + requestHost + ", " + url + ": " + action);
}
return action;
}
Expand Down Expand Up @@ -443,6 +449,34 @@ function getSocialWidgetBlockList(tabId) {
};
}

// Check if tab is temporarily unblocked for tracker
function isSocialWidgetTemporaryUnblock(tabId, url, frameId) {
var exceptions = temporarySocialWidgetUnblock[tabId];
if (exceptions == undefined) {
return false;
}

var requestHost = extractHostFromURL(url);
var requestExcept = (exceptions.indexOf(requestHost) != -1);

var frameHost = extractHostFromURL(getFrameUrl(tabId, frameId));
var frameExcept = (exceptions.indexOf(frameHost) != -1);

//console.log((requestExcept || frameExcept) + " : exception for " + url);

return (requestExcept || frameExcept);
}

// Unblocks a tracker just temporarily on this tab, because the user has clicked the
// corresponding replacement social widget.
function unblockSocialWidgetOnTab(tabId, socialWidgetUrl) {
if (temporarySocialWidgetUnblock[tabId] == undefined){
temporarySocialWidgetUnblock[tabId] = [];
}
var socialWidgetHost = extractHostFromURL(socialWidgetUrl);
temporarySocialWidgetUnblock[tabId].push(socialWidgetHost);
}

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
var tabHost = extractHostFromURL(sender.tab.url);
Expand All @@ -456,5 +490,10 @@ chrome.runtime.onMessage.addListener(
var socialWidgetBlockList = getSocialWidgetBlockList(sender.tab.id);
sendResponse(socialWidgetBlockList);
}
if(request.unblockSocialWidget){
var socialWidgetUrl = request.buttonUrl;
unblockSocialWidgetOnTab(sender.tab.id, socialWidgetUrl);
sendResponse();
}
}
);