Skip to content

Commit

Permalink
feat: remove 'tweet your reply' button (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber authored Aug 19, 2021
1 parent 3fb39c9 commit dc0236d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/cdp-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,32 @@ export const querySelector = async (
throw new Error('Not found');
}

return nodeId;
return nodeId as number;
},
{
retries: 3,
onFailedAttempt: async () => await sleep(100),
},
);

export const hideNode = async (
export const xpath = async (
DOM,
queryNodeId: number,
selector: string,
query: string,
) => {
const nodeId = await querySelector(
DOM,
queryNodeId,
selector,
);
const { searchId, resultCount } = await DOM.performSearch({ query });
const { nodeIds } = await DOM.getSearchResults({
searchId,
fromIndex: 0,
toIndex: resultCount,
});

return nodeIds as number[];
};

export const hideNode = async (
DOM,
nodeId: number,
) => {
await DOM.setAttributeValue({
nodeId,
name: 'style',
Expand Down
9 changes: 7 additions & 2 deletions src/tweet-camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CDP from 'chrome-remote-interface';
import exitHook from 'exit-hook';
import {
querySelector,
xpath,
waitForNetworkIdle,
hideNode,
screenshotNode,
Expand Down Expand Up @@ -156,12 +157,16 @@ class TweetCamera {
const { root } = await client.DOM.getDocument();
const tweetContainerNodeId = await querySelector(client.DOM, root.nodeId, '#app > div > div > div:last-child');

const [tweetLinkNodeId] = await xpath(client.DOM, '//a[starts-with(@href, "https://twitter.com/intent/tweet")]/..');

await Promise.all([
// Share tweet button - can't use aria-label because of i18n
hideNode(client.DOM, tweetContainerNodeId, '[role="button"][aria-label]'),
hideNode(client.DOM, await querySelector(client.DOM, tweetContainerNodeId, '[role="button"][aria-label]')),

// Info button - can't use aria-label because of i18n
hideNode(client.DOM, tweetContainerNodeId, 'a[href$="twitter-for-websites-ads-info-and-privacy"]'),
hideNode(client.DOM, await querySelector(client.DOM, tweetContainerNodeId, 'a[href$="twitter-for-websites-ads-info-and-privacy"]')),

client.DOM.removeNode({ nodeId: tweetLinkNodeId }),

// Unset max-width to fill window width
client.DOM.setAttributeValue({
Expand Down

0 comments on commit dc0236d

Please sign in to comment.