For the last clean corner of my phone, I will fight. (ง •_•)ง
Why this became necessary
For various reasons, I still occasionally want to browse Baidu Tieba. Some large companies may behave in unpleasant ways, but precisely because they are large, their platforms have accumulated a huge amount of information over the years. Sometimes the thing you are looking for really is there.
About a year ago, I had already run into a Baidu-related problem: search results would not open properly when I used Baidu Search, so I had to find a workaround. Later on, Baidu seemed to fix that issue themselves, probably because they also realized that breaking search results was not exactly good for their own reputation.
This time, the problem is Tieba. Installing the official Tieba app, however, would feel like an insult to my phone. Normally, the simple answer would be to use the desktop version of Tieba in a browser. Unfortunately, the browser I use is Firefox Rocket, which is based on WebView and does not support changing the user agent. So if the browser cannot solve it, programming will have to.
The actual problem
When opening Tieba threads on a phone in the normal mobile page, the content is incomplete. You can usually see only the first two floors. A forum that only lets you read two floors is, of course, basically useless. If you want to continue reading, it pushes you toward downloading the Tieba app.
Tieba used to have an extremely minimal version. It was crude, but good enough for reading. The bad news is that Baidu no longer allows that minimal version to list thread titles. In other words, if you open a forum through the minimal interface, you cannot see the thread list.
But there is still a usable gap: if you already know a thread ID, the old minimal page appears to open the thread normally. So the whole task becomes simple: get the thread list, extract the thread IDs, and send those IDs to the minimal Tieba URL.
After some testing, adding a thread ID after this address allows the full thread content to be viewed:
http://tieba.baidu.com/mo/q-----1----/m?kz=
It does not seem to support posting, but I only need to read, so that is not a problem.
With that in mind, and after referring to some old code from an Iwara Viewer project, I ended up with this PHP script.
Code
<?php
if (isset($_GET["kw"])) {
if (!isset($_GET["pn"])) {
$_GET["pn"] = 1;
}
$url = 'https://tieba.baidu.com/f?kw='.iconv("utf-8","gb2312",$_GET["kw"]).'&pn='.($_GET["pn"]-1)*50;
$str = file_get_contents($url);
$preg='/<a rel="noreferrer" href="\/p\/(.*?)" title="(.*?)" target="_blank" class="j_th_tit ">/is';
preg_match_all($preg,$str,$match);//在$str中搜索匹配所有符合$preg加入$match中
echo "<title>Mayx Tieba Viewer</title><h1>Mayx Tieba Viewer</h1><hr />";
if ($http_response_header[0] != "HTTP/1.0 200 OK"){
echo "<b>Warning</b>:It's Not Tieba Name<br>";
}
for($i=0;$i<count($match[0]);$i++)//逐个输出超链接地址
{
echo "<a href=\"http://tieba.baidu.com/mo/q-----1----/m?kz=".$match[1][$i]."\">".$match[2][$i]."</a><br>";
}
echo "<hr><a href=\"?pn=".($_GET["pn"] + 1)."&kw=".$_GET["kw"]."\" >Next Page</a>";
} else {
echo '<title>Mayx Tieba Viewer</title><h1>Mayx Tieba Viewer</h1><hr><form action="" method="get">Please Input Tieba Name:<input type="text" name="kw" required><input type="submit" value="Submit"></form>';
}
JavaScript could probably do something similar, but my JavaScript is not good enough, so PHP it is.
A few notes
This little thing is designed specifically for mobile use. On a computer, there is no real need to read Tieba this way.
What is frustrating is that plain HTML is perfectly capable of providing the needed reading experience. Forcing people to install an app may make sense from a business perspective, but it is still extremely unpleasant as a user. Zhihu has similar problems too. If a product becomes too annoying, users will eventually leave.
The funniest part is that I later realized Tieba is available inside some mini programs as well, including on WeChat. So in a sense, this was unnecessary work. Whatever—at least it was good practice, and it produced something to write about. 😂