Skip to content

Commit

Permalink
[Fix/Feature] Allow main element to be specified as an array of selec…
Browse files Browse the repository at this point in the history
…tors + fixed urlData for FB. Fixes #218 and #219.
  • Loading branch information
tanushree0 committed Dec 10, 2013
1 parent d3dcf6d commit acd5139
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 15 deletions.
42 changes: 38 additions & 4 deletions background_scripts/data/urlDataMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ urlData objects associated with a URL's domain takes O(1) time, and search for t
the URL is then restricted to the (very small) array.
ii) it results in better structure/organization compared to having arrays of regexps at the top level.
6) Anywhere a selector is specified, the extended set of jQuery selectors can be used as well.
6)
i) Anywhere a selector is specified, the extended set of jQuery selectors can be used as well.
ii) A selector can be specified as a string or an array of strings. When specified as an array, we consider them in order
till an element is found.
7) // Guide for standard ("std_") items in urlData:
This applies to SUs and actions (both within page and CU levels), whose names begin with the prefix "std_"
Expand Down Expand Up @@ -267,6 +270,11 @@ defaultSettings.urlDataMap = {
}
},

"units.io": {
urlPatterns:["blog.units.io*"],
CUs_specifier: ".post"
},

"boredpanda.com": [
{
urlPatterns: ["www.boredpanda.com"],
Expand Down Expand Up @@ -339,7 +347,10 @@ defaultSettings.urlDataMap = {
{
// Facebook main feed page
urlPatterns: ["www.facebook.com", "www.facebook.com/?ref=logo", "www.facebook.com/groups/*", "www.facebook.com/hashtag/*"],
CUs_specifier: ".genericStreamStory.uiUnifiedStory, ._6kq", // ._6kq for the new layout
CUs_specifier: ".genericStreamStory.uiUnifiedStory, " + // For the original layout. This selector has consistently
// worked since Nov 2012. Possibly still exists for some users.
"._5uch._5jmm._5pat, " + // Related to changes made around Nov 2013
"._6kq,", // for the new layout, that has a very limited release at the moment.
CUs_SUs: {
// The last selector in the following apply for the new FB layout (for eg: ._6k6, ._6k2 etc)
"std_upvote": {kbdShortcuts: ["l", "u"], selector: ".UFILikeLink, ._6k6" },
Expand All @@ -365,10 +376,33 @@ defaultSettings.urlDataMap = {
// NOTE: We can afford for these selectors to be non-optimized because these will be looked for inside $CU.
// If these were meant for the entire page, then some of these would be very bad!

std_mainEl: ".fbMainStreamAttachment a:first-child:not(.highlightSelectorButton, .fbQuestionPollForm a, ._4q5, .lfloat, .shareRedesignContainer>a), " +
std_mainEl: [
// mainEl is specified as an array of selectors for FB in order of preference.
// The first selector is for shared content, the second for the author of the post.


// 1. Shared content

// original FB layout (was working consistently since Nov 2012)
".fbMainStreamAttachment a:first-child:not(.highlightSelectorButton, .fbQuestionPollForm a, ._4q5, .lfloat, .shareRedesignContainer>a), " +
".uiStreamAttachments a:not(.highlightSelectorButton, .fbQuestionPollForm a, ._4q5, .lfloat, .shareRedesignContainer>a), " +
".uiStreamSubstory .pronoun-link, .shareText a, a.shareText, " +
"a._4-eo, ._6m3 a, a._52c6, a._6ki, a._6k_", // these are for the new FB layout


// Since Nov 2013, some users' FB has the selectors changed.
"a.uiVideoThumb, " +
"a._5pb3._5dec, " + // image of shared link
"a._5pc0._5dec, ._5pc1._5dec a, " + // shared photo
"._5pb_.mvm>a, " + // photo. Not a directly "shared" photo, generally a photo that a friend commented on.
"._5pc1._5dec.mbs.mrs a" + // photo in a shared album

// Latest FB layout. Rolled out to a very limited set of users.
"a._4-eo, ._6m3 a, a._52c6, a._6ki, a._6k_",


// 2. Author of post (default main element if shared content not present)

".fwb.fcg a, a.profileLink"],

std_seeMore: ".text_exposed_link>a"
},
Expand Down
16 changes: 8 additions & 8 deletions background_scripts/mod_settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,13 @@ _u.mod_settings = (function($, mod_commonHelper, mod_getMainDomain, defaultSetti
// Also adds default 'miniDesc' and 'kbdShortcuts' values, if not specified by SUs/actions defined in urlData
function expandUrlData(urlData) {

// if key value at property 'key' in object 'obj' is a string, it is expanded to point to an object having a property
// 'selector' that points to the string instead.
var expandPropertyToObjIfString = function(obj, key) {
var str;
if (typeof (str = obj[key]) === "string") {
// if key value at property 'key' in object 'obj' is a string or an Array, it is expanded to point to an object
// having a property 'selector' that points to the string instead.
var expandPropertyToObjIfStringOrArray = function(obj, key) {
var value;
if (typeof (value = obj[key]) === "string" || Array.isArray(value)) {
obj[key] = {
selector: str
selector: value
};
}
};
Expand All @@ -581,13 +581,13 @@ _u.mod_settings = (function($, mod_commonHelper, mod_getMainDomain, defaultSetti
var expandSUsOrActions = function(SUsOrActions, scope) {
if (typeof SUsOrActions === "object") {
for (var SUorAction_Name in SUsOrActions) {
expandPropertyToObjIfString(SUsOrActions, SUorAction_Name);
expandPropertyToObjIfStringOrArray(SUsOrActions, SUorAction_Name);
supplementWithDefaultValues(SUsOrActions[SUorAction_Name], SUorAction_Name, scope);
}
}
};

expandPropertyToObjIfString(urlData, 'CUs_specifier');
expandPropertyToObjIfStringOrArray(urlData, 'CUs_specifier');

expandSUsOrActions(urlData.CUs_SUs, "CUs");
expandSUsOrActions(urlData.CUs_actions, "CUs");
Expand Down
28 changes: 25 additions & 3 deletions content_scripts/mod_CUsMgr.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,33 @@ _u.mod_CUsMgr = (function($, mod_basicPageUtils, mod_domEvents, mod_keyboardLib,
*/
function getMainElement($CU) {
var $mainElement;
mainElementSelector && ($mainElement = $CU.find(mainElementSelector));
if ($mainElement && $mainElement.length) {
return $mainElement[0];

if (mainElementSelector) {

// If the mainElementSelector is an array, then check the selectors in order till a mainElement is found.
if (Array.isArray(mainElementSelector)) {
var i = 0;

while (!($mainElement && $mainElement.length) && i <= mainElementSelector.length) {
$mainElement = $CU.find(mainElementSelector[i]);
i++;
}
}
else {
$mainElement = $CU.find(mainElementSelector);
}

if ($mainElement && $mainElement.length) {
return $mainElement[0];
}

}

// mainElementSelector && ($mainElement = $CU.find(mainElementSelector));
// if ($mainElement && $mainElement.length) {
// return $mainElement[0];
// }

// If main element not specified or found, then return the first focusable in the CU.
var $containedFocusables = $getContainedFocusables($CU).filter(function() {
return mod_contentHelper.elemAllowsSingleKeyShortcut(this);
Expand Down

0 comments on commit acd5139

Please sign in to comment.