diff --git a/manifest.json b/manifest.json index a4adac2..147d745 100644 --- a/manifest.json +++ b/manifest.json @@ -41,7 +41,8 @@ "modules/9gag.js", "modules/imgur.js", "modules/twitter.js", - "modules/douyin.js" + "modules/douyin.js", + "modules/threads.js" ] } } diff --git a/modules/threads.js b/modules/threads.js new file mode 100644 index 0000000..e742a48 --- /dev/null +++ b/modules/threads.js @@ -0,0 +1,45 @@ +zeeschuimer.register_module( + 'Threads', + 'threads.net', + function (response, source_platform_url, source_url) { + let domain = source_platform_url.split("/")[2].toLowerCase().replace(/^www\./, ''); + + if ( + !["threads.net"].includes(domain) + || ( + // these are known API endpoints used to fetch posts for the interface + source_url.indexOf('/api/graphql') < 0 + ) + ) { + return []; + } + + let data; + try { + data = JSON.parse(response); + } catch (SyntaxError) { + return []; + } + + let posts = []; + let traverse = function (obj) { + for (let property in obj) { + let child = obj[property]; + if(!child) { + continue; + } + if(child.hasOwnProperty('thread_items') && child['thread_items']) { + child['thread_items'].forEach((item) => { + item['id'] = item['post']['id']; + posts.push(item) + }); + } else if (typeof (child) === "object") { + traverse(child); + } + } + } + + traverse(data); + return posts; + } +); \ No newline at end of file