Skip to content

Commit

Permalink
fix tweet page and tweet replies no longer working (#83)
Browse files Browse the repository at this point in the history
* fix tweet page and tweet replies no longer working

* bump manifest versions to 0.2.5

* the error I added for testing was still there
  • Loading branch information
kheina authored May 8, 2023
1 parent 2d093d6 commit e902fbe
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 47 deletions.
Binary file added assets/error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion firefox-manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Blue Blocker",
"version": "0.2.4",
"version": "0.2.5",
"description": "Blocks all Twitter Blue verified users on twitter.com",
"icons": {
"128": "assets/icon-128.png"
Expand Down
4 changes: 2 additions & 2 deletions injected/inject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const RequestRegex = /^https:\/\/(?:\w+\.)?twitter.com\/[\w\/\.]+\/(HomeLatestTimeline|HomeTimeline|UserTweets|timeline\/home\.json|TweetDetail|search\/typeahead\.json|search\/adaptive\.json)(?:$|\?)/;

(function(xhr) {
const RequestRegex = /^https:\/\/(?:\w+\.)?twitter.com\/[\w\/\.\-\_\=]+\/(HomeLatestTimeline|HomeTimeline|UserTweets|timeline\/home\.json|TweetDetail|search\/typeahead\.json|search\/adaptive\.json)(?:$|\?)/;

let XHR = XMLHttpRequest.prototype;
let open = XHR.open;
let send = XHR.send;
Expand Down
7 changes: 7 additions & 0 deletions injected/toasts.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
box-shadow: 0 2px 3px 1px var(--shadowcolor);
min-height: calc(2em + 4px);
}
#injected-blue-block-toasts .toast.error {
background: url('../assets/error.png') var(--bg1color);
background-repeat: no-repeat;
background-size: 2.5em;
background-position-x: 1em;
background-position-y: center;
}

#injected-blue-block-toasts .toast a,
#injected-blue-block-toasts .toast a:active,
Expand Down
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ firefox:
zip "blue-blocker-firefox-${VERSION}.zip" \
assets/icon-128.png \
assets/icon.png \
assets/error.png \
injected/* \
models/* \
parsers/* \
Expand All @@ -31,6 +32,7 @@ chrome:
zip "blue-blocker-chrome-${VERSION}.zip" \
assets/icon-128.png \
assets/icon.png \
assets/error.png \
injected/* \
models/* \
parsers/* \
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Blue Blocker",
"version": "0.2.4",
"version": "0.2.5",
"description": "Blocks all Twitter Blue verified users on twitter.com",
"icons": {
"128": "assets/icon-128.png"
Expand Down
9 changes: 9 additions & 0 deletions models/block_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ export class BlockQueue {
this.releaseCriticalPoint();
return item;
}
async clear() {
await this.getCriticalPoint();
const items = await this.storage.get({ BlockQueue: [] });
if (!items.BlockQueue || items.BlockQueue.length === 0) {
return;
}
await this.storage.set({ BlockQueue: [] });
this.releaseCriticalPoint();
}
}
20 changes: 10 additions & 10 deletions parsers/instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,13 @@ export function ParseTimelineTweet(tweet, headers) {
export function HandleInstructionsResponse(e, body) {
// pull the "instructions" object from the tweet
let instructions = body;

try {
for (const key of InstructionsPaths[e.detail.parsedUrl[1]]) {
instructions = instructions[key];
}
}
catch (e) {
console.error(logstr, "failed to parse response body for instructions object", e, body);
return;

for (const key of InstructionsPaths[e.detail.parsedUrl[1]]) {
instructions = instructions[key];
}

console.debug(logstr, "parsed instructions path:", instructions);

// "instructions" should be an array, we need to iterate over it to find the "TimelineAddEntries" type
let tweets = undefined;
let isAddToModule = false;
Expand Down Expand Up @@ -140,7 +136,11 @@ export function HandleInstructionsResponse(e, body) {

default:
if (!IgnoreTweetTypes.has(tweet.content.entryType)) {
console.error(logstr, `unexpected tweet type found: ${tweet.content.entryType}`, tweet);
throw {
message: `unexpected tweet type found: ${tweet?.content?.entryType}`,
name: "TweetType",
tweet,
};
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ <h2><img src="../assets/icon.png" alt="🅱️">lue Blocker</h2>
<span id="block-interval-status"></span>
</div>
<div class="slider">
<input type="range" min="5" max="60" value="5" id="block-interval">
<input type="range" min="5" max="60" value="15" id="block-interval">
<span id="block-interval-value"></span>
</div>
</div>
Expand Down
38 changes: 22 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { api, logstr } from './constants.js';
import { ClearCache } from './shared.js';
import { api } from './constants.js';
import { ClearCache, ErrorEvent, EventKey } from './shared.js';
import { HandleInstructionsResponse } from './parsers/instructions.js';
import { HandleForYou } from './parsers/timeline.js';
import { HandleTypeahead } from './parsers/search.js';
Expand All @@ -23,19 +23,25 @@ document.addEventListener("blue-blocker-event", function (e) {
// TODO: we may want to seriously consider clearing the cache on a much less frequent
// cadence since we're no longer able to block users immediately and need the queue
ClearCache();
const body = JSON.parse(e.detail.body);
switch (e.detail.parsedUrl[1]) {
case "HomeLatestTimeline":
case "HomeTimeline":
case "UserTweets":
case "TweetDetail":
return HandleInstructionsResponse(e, body);
case "timeline/home.json":
case "search/adaptive.json":
return HandleForYou(e, body);
case "search/typeahead.json":
return HandleTypeahead(e, body);
default:
console.error(logstr, "found an unexpected url that we don't know how to handle:", e.detail.url);
let body = null;
try {
body = JSON.parse(e.detail.body);
switch (e.detail.parsedUrl[1]) {
case "HomeLatestTimeline":
case "HomeTimeline":
case "UserTweets":
case "TweetDetail":
return HandleInstructionsResponse(e, body);
case "timeline/home.json":
case "search/adaptive.json":
return HandleForYou(e, body);
case "search/typeahead.json":
return HandleTypeahead(e, body);
default:
api.storage.local.set({ [EventKey]: { type: ErrorEvent, message: "found an unexpected url that we don't know how to handle", detail: e } });
}
}
catch (error) {
api.storage.local.set({ [EventKey]: { type: ErrorEvent, message: "exepected error occurred while parsing request body", detail: { error, body, event: e } } });
}
});
54 changes: 38 additions & 16 deletions shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ function unblockUser(user, user_id, headers, reason) {
ajax.send(formdata);
}

const eventKey = "MultiTabEvent";
export const EventKey = "MultiTabEvent";
export const ErrorEvent = "ErrorEvent";
const UserBlockedEvent = "UserBlockedEvent";
api.storage.local.onChanged.addListener(items => {
// we're using local storage as a really dirty event driver
if (!items.hasOwnProperty(eventKey)) {
if (!items.hasOwnProperty(EventKey)) {
return;
}
const e = items[eventKey].newValue;
const e = items[EventKey].newValue;

switch (e.type) {
case UserBlockedEvent:
Expand All @@ -106,6 +107,20 @@ api.storage.local.onChanged.addListener(items => {
}
break;

case ErrorEvent:
// skip checking options, since errors should always be shown
const { message, detail } = e;
console.error(logstr, `${message}:`, detail);

const t = document.createElement("div");
t.className = "toast error";
t.innerHTML = `<p>an error occurred! check the console and create an issue on <a href="https://github.com/kheina-com/Blue-Blocker/issues" target="_blank">GitHub</a></p>`;

const ele = document.getElementById("injected-blue-block-toasts");
ele.appendChild(t);
setTimeout(() => ele.removeChild(t), 60e3);
break;

default:
console.error(logstr, "unknown multitab event occurred:", e);
}
Expand All @@ -129,18 +144,25 @@ function QueueBlockUser(user, user_id, headers, reason) {
}

function CheckBlockQueue() {
api.storage.sync.get(DefaultOptions).then(items => {
SetOptions(items);
})
.then(() => queue.shift())
.then(item => {
if (item === undefined) {
consumer.stop();
return;
}
const {user, user_id, headers, reason} = item;
BlockUser(user, user_id, headers, reason);
});
let event = null;
try {
api.storage.sync.get(DefaultOptions).then(items => {
SetOptions(items);
})
.then(() => queue.shift())
.then(item => {
event = item;
if (item === undefined) {
consumer.stop();
return;
}
const {user, user_id, headers, reason} = item;
BlockUser(user, user_id, headers, reason);
});
}
catch (error) {
api.storage.local.set({ [EventKey]: { type: ErrorEvent, message: "unexpected error occurred while processing block queue", detail: { error, event } } });
}
}

const consumer = new QueueConsumer(api.storage.local, CheckBlockQueue, async s => {
Expand Down Expand Up @@ -171,7 +193,7 @@ function BlockUser(user, user_id, headers, reason, attempt=1) {
else {
blockCounter.increment();
console.log(logstr, `blocked ${user.legacy.name} (@${user.legacy.screen_name}) due to ${ReasonMap[reason]}.`);
api.storage.local.set({ [eventKey]: { type: UserBlockedEvent, user, user_id, headers, reason } })
api.storage.local.set({ [EventKey]: { type: UserBlockedEvent, user, user_id, headers, reason } })
}
});
ajax.addEventListener('error', error => {
Expand Down

0 comments on commit e902fbe

Please sign in to comment.