This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial reddit behavior implementation fixes #24
- Loading branch information
Showing
12 changed files
with
446 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as lib from '../../lib'; | ||
|
||
export const ReditContainerSelector = 'div[id*="container" i]'; | ||
export const CloseButtonSelector = 'button[aria-label="close" i] > span'; | ||
export const APost = 'div[class*="post" i]'; | ||
export const APostVideo = 'video'; | ||
export const APostTitleAndViewClicker = 'a[data-click-id="body"]'; | ||
export const APostMediaEmbed = 'iframe[class*="media-element" i]'; | ||
export const MaybePromotedSpan = 'div > span'; | ||
export const MoreCommentsDivP = 'div[id*="moreComments-" i] > * > p'; | ||
export const ViewedPostOverlayScrollContainerId = 'overlayScrollContainer'; | ||
|
||
export function createSubPartsSelector() { | ||
const theSub = lib.substringFromIndexOf(location.pathname, '/r/'); | ||
return `div[class="SubredditVars-r-${theSub.replace('/', '')}" i]`; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import * as lib from '../../lib'; | ||
import * as selectors from './selectors'; | ||
|
||
/** | ||
* | ||
* @return {SubStructure} | ||
*/ | ||
export function getSubStructure() { | ||
let header; | ||
let body; | ||
let bottomMostSubVar; | ||
if (location.pathname.startsWith('/r/')) { | ||
const parts = lib.qsa(selectors.createSubPartsSelector()); | ||
header = parts[0]; | ||
body = parts[1]; | ||
bottomMostSubVar = parts[2]; | ||
} else { | ||
const container = lib.chainFistChildElemOf( | ||
lib.qs(selectors.ReditContainerSelector), | ||
3 | ||
); | ||
for (let i = 0; i < container.children.length; i++) { | ||
const child = container.children[i]; | ||
if (child instanceof HTMLDivElement) { | ||
if (header == null) header = child; | ||
else if (body == null) body = child; | ||
else if (bottomMostSubVar == null) bottomMostSubVar = child; | ||
} | ||
} | ||
} | ||
// loader > div > div > div.Post | ||
const postList = lib.getNthParentElement(lib.qs(selectors.APost, body), 3); | ||
return { | ||
header, | ||
body, | ||
postList, | ||
bottomMostSubVar, | ||
}; | ||
} | ||
|
||
/** | ||
* | ||
* @param {?Element} postContainer | ||
* @return {boolean} | ||
*/ | ||
export function isNotPromotedOrAddPost(postContainer) { | ||
const maybePromotedSpan = lib.qs(selectors.MaybePromotedSpan, postContainer); | ||
if (maybePromotedSpan) { | ||
const promoted = lib.elementTextContains( | ||
maybePromotedSpan, | ||
'promoted', | ||
true | ||
); | ||
if (promoted) lib.scrollIntoView(postContainer); | ||
return !promoted; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @param {Element} postContainer | ||
* @return {?Element} | ||
*/ | ||
export function selectPost(postContainer) { | ||
return lib.qs(selectors.APost, postContainer); | ||
} | ||
|
||
/** | ||
* @param {Element} postContainer | ||
* @return {{postContainer: Element, post: Element}} | ||
*/ | ||
export function selectPostWithContainer(postContainer) { | ||
return { | ||
postContainer, | ||
post: lib.qs(selectors.APost, postContainer), | ||
}; | ||
} | ||
|
||
/** | ||
* | ||
* @param {Element} bottomMostSubVar | ||
* @return {?Element} | ||
*/ | ||
export function findPostViewer(bottomMostSubVar) { | ||
if (bottomMostSubVar.hasChildNodes()) { | ||
return bottomMostSubVar; | ||
} | ||
return lib.getNthPreviousElementSibling(bottomMostSubVar, 2); | ||
} | ||
|
||
export async function* loadAllComments() { | ||
const commentLoader = lib.chainFistChildElemOf( | ||
lib.lastChildElementOf( | ||
lib.chainFistChildElemOf( | ||
lib.id(selectors.ViewedPostOverlayScrollContainerId), | ||
3 | ||
) | ||
), | ||
3 | ||
); | ||
yield lib.stateWithMsgNoWait('Loading comments'); | ||
let currentMoreComments = lib.qsa( | ||
selectors.MoreCommentsDivP, | ||
commentLoader | ||
); | ||
while (currentMoreComments.length) { | ||
for (let i = 0; i < currentMoreComments.length; i++) { | ||
await lib.scrollIntoViewAndClickWithDelay(currentMoreComments[i]); | ||
} | ||
yield lib.stateWithMsgWait( | ||
`Clicked ${currentMoreComments.length} load more comments elements` | ||
); | ||
currentMoreComments = lib.qsa( | ||
selectors.MoreCommentsDivP, | ||
commentLoader | ||
); | ||
} | ||
yield lib.stateWithMsgNoWait('Loaded all comments'); | ||
} | ||
|
||
/** | ||
* @typedef {Object} SubStructure | ||
* @property {Element} header | ||
* @property {Element} body | ||
* @property {Element} postList | ||
* @property {Element} bottomMostSubVar | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import * as lib from '../../lib'; | ||
import * as selectors from './selectors'; | ||
import * as shared from './shared'; | ||
|
||
/** | ||
* | ||
* @param {Element} post | ||
* @param {{subStructure: SubStructure, cliAPI: Object}} args | ||
* @return {AsyncIterableIterator<*>} | ||
*/ | ||
async function* viewPost(post, { subStructure }) { | ||
const postTileAndClicker = lib.qs(selectors.APostTitleAndViewClicker, post); | ||
const postTile = lib.elemInnerText(postTileAndClicker) || 'some post'; | ||
yield lib.stateWithMsgNoWait(`Viewing: ${postTile}`); | ||
await lib.scrollIntoViewWithDelay(post); | ||
const postVideo = lib.qs(selectors.APostVideo, post); | ||
if (postVideo) { | ||
yield lib.stateWithMsgWaitFromAwaitable( | ||
lib.noExceptPlayMediaElement(postVideo), | ||
'Playing posts video' | ||
); | ||
} | ||
await lib.clickWithDelay(postTileAndClicker || post); | ||
const view = lib.getNthPreviousElementSibling( | ||
subStructure.bottomMostSubVar, | ||
2 | ||
); | ||
if (view) { | ||
await lib.selectElemFromAndClickWithDelay( | ||
view, | ||
selectors.CloseButtonSelector | ||
); | ||
} else { | ||
yield lib.stateWithMsgNoWait('not a post'); | ||
} | ||
} | ||
|
||
export default async function* postIterator(cliAPI) { | ||
const subStructure = shared.getSubStructure(); | ||
yield* lib.traverseChildrenOf2({ | ||
parentElement: subStructure.postList, | ||
handler: viewPost, | ||
additionalArgs: { subStructure, cliAPI }, | ||
loader: true, | ||
filter: shared.isNotPromotedOrAddPost, | ||
selector: shared.selectPost, | ||
}); | ||
} | ||
|
||
export const metaData = { | ||
name: 'subRedditBehavior', | ||
description: 'Capture all posts on sub-reddits page.', | ||
match: { | ||
regex: /^https:\/\/(www\.)?reddit\.com\/r\/[^/]+(?:\/(?:[a-z]+\/?))?$/, | ||
} | ||
}; | ||
|
||
export const isBehavior = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import postIterator from './sub'; | ||
|
||
export default postIterator; | ||
|
||
export const metaData = { | ||
name: 'redditTimelineBehavior', | ||
description: 'Capture all posts on reddits main page.', | ||
match: { | ||
regex: /^https:\/\/(www\.)?reddit\.com(?:(?:\/[a-z]{3, }\/?)|(?:\/))?$/, | ||
}, | ||
}; | ||
|
||
export const isBehavior = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.