Skip to content

Commit

Permalink
🔥 Fix Error Finding Tweets (#37)
Browse files Browse the repository at this point in the history
* Add a few more console.logs for debugging

* Prefix all console.logs so they're easier to find

* 🤦‍♂️ Fix container selector to match Twitter's updated timeline markup

* Update changelog

* v2.1.5
  • Loading branch information
tannerhodges authored Jul 3, 2023
1 parent 4171dfb commit 236dba7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.1.5] - 2023-07-01

### Fixed

- 🔥 Fix error finding tweets.

## [2.1.4] - 2022-01-03

### Fixed
Expand Down Expand Up @@ -85,7 +91,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Automatically print when replies have finished loading.
- Hide promoted tweets.

[Unreleased]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.4...HEAD
[Unreleased]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.5...HEAD
[2.1.5]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.4...v2.1.5
[2.1.4]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.3...v2.1.4
[2.1.3]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.2...v2.1.3
[2.1.2]: https://github.com/tannerhodges/twitter-print-styles/compare/v2.1.1...v2.1.2
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "twitter-print-styles",
"version": "2.1.4",
"version": "2.1.5",
"description": "Chrome extension for adding simple print styles to save Twitter threads as PDFs.",
"keywords": [
"chrome",
Expand Down
24 changes: 13 additions & 11 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function getAllCSS(doc = document) {
.map(rule => rule.cssText)
.join('');
} catch (e) {
console.log('Access to stylesheet %s is denied. Ignoring...', styleSheet.href);
console.log('[twitter-print-styles] Access to stylesheet %s is denied. Ignoring...', styleSheet.href);
}
})
.filter(Boolean)
Expand Down Expand Up @@ -164,7 +164,7 @@ chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {

// Multiple clicks cancels out any running tasks.
if (running) {
console.log('Cancelling current task...');
console.log('[twitter-print-styles] Cancelling current task...');
keepRunning = false;
return;
}
Expand All @@ -185,17 +185,19 @@ Dev Tools > More Tools > Rendering > Emulate CSS media type
// NOTE: While several views have "Timelines", this is
// only expected to work on _Tweets_ & _Threads_.
const timeline = document.querySelector('[data-testid="primaryColumn"] [role="region"] > div');
console.log('[twitter-print-styles] = timeline', timeline);

if (!timeline) {
console.log('Could not find Twitter timeline.');
console.log('[twitter-print-styles] Could not find Twitter timeline.');
return;
}

running = true;
keepRunning = true;

// Find the tweets.
const container = timeline.firstElementChild;
const container = timeline.querySelector('[style*="position: relative"]');
console.log('[twitter-print-styles] container =', container);

// Make sure we're actually on the first one.
window.scrollTo(0, 0);
Expand All @@ -205,6 +207,7 @@ Dev Tools > More Tools > Rendering > Emulate CSS media type
let tweet = container.firstElementChild;
let count = 0;
let error = false;
console.log('[twitter-print-styles] tweet = ', tweet);

// Since Twitter only shows a few tweets in the DOM at a time, we need to
// cache them in a variable so we can print them all together.
Expand Down Expand Up @@ -241,7 +244,7 @@ Dev Tools > More Tools > Rendering > Emulate CSS media type
// Increment tweet count.
count += 1;

console.log(`Tweet #${count}`, tweet);
console.log(`[twitter-print-styles] Tweet #${count}`, tweet);

// Scroll tweet to the top of the screen (to trigger Twitter's "load more" action).
tweet.scrollIntoView(true);
Expand All @@ -257,8 +260,7 @@ Dev Tools > More Tools > Rendering > Emulate CSS media type

tweet = await waitUntil(async (resolve) => {
const nextTweet = tweet.nextElementSibling;

console.log('Finding next tweet...');
console.log('[twitter-print-styles] Finding next tweet...', nextTweet);

// If we fail to find a tweet within 10 seconds, something's probably gone wrong.
// Assume the worst and abort.
Expand All @@ -282,18 +284,18 @@ https://github.com/tannerhodges/twitter-print-styles/issues`);
}

if (!nextTweet) {
console.log('Tweet not found.', tweet);
console.log('[twitter-print-styles] Tweet not found.', tweet);
return;
}

if (isLastTweet(nextTweet)) {
console.log('--- THE END ---');
console.log('[twitter-print-styles] --- THE END ---');
resolve(false);
return;
}

if (isMoreReplies(nextTweet)) {
console.log('Loading more replies...');
console.log('[twitter-print-styles] Loading more replies...');
const moreReplies = nextTweet.querySelector('[role="button"]');
moreReplies.click();
return;
Expand All @@ -315,7 +317,7 @@ https://github.com/tannerhodges/twitter-print-styles/issues`);
return;
}

console.log('PRINT');
console.log('[twitter-print-styles] PRINT');

// Copy the Twitter timeline into a new window.
const clone = document.querySelector('[data-testid="primaryColumn"]').cloneNode(true);
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Twitter Print Styles",
"version": "2.1.4",
"version": "2.1.5",
"description": "Simple print styles for saving Twitter threads as PDFs.",
"icons": {
"16": "icon-16.png",
Expand Down

0 comments on commit 236dba7

Please sign in to comment.