Wednesday, April 5, 2017

How to Sync your Desktop Chrome Bookmarks with Third-Party Chrome Browsers on Android

Over the past year, third-party browsers based on Google’s Chrome have exploded in popularity. This trend started after the Code Aurora Forum (CAF) began publishing Chromium builds optimized for Snapdragon devices in October of 2015. Developers interested in making the ultimate, feature-filled browser began forking the project and added features such as a night mode, content blockers, power saving modes, gesture support, and more on top of support for Chrome bookmarks through the Chrome Sync API.

Users flocked to projects such as the popular TugaBrowser on our forums because these browsers offered many enhancements on top of Google’s existing Chrome sync features that isn’t available in non-Chrome browsers. Unfortunately, Google shut down third-party access to the Chrome Sync API back in January, citing a security concern. Google stated that the move was not intentionally made to hamper third-party Chrome browsers, for which even the open-source Chromium is classified as, but this security patch incidentally spelled the end of bookmark syncing for third-party Chrome browsers.

At this time, there is still no way for browsers such as TugaBrowser to synchronize bookmark changes with Chrome Sync. But there is a way to sync your desktop bookmarks with third-party Chrome browsers, provided you have root access. The method, which we’ve posted about before, can basically be summarized in these steps:

  1. Open the bookmarks file for your third-party Chrome browser and copy the checksum value
  2. Push the bookmarks file from Chrome’s directory to your third-party Chrome browser’s directory, overwriting it.
  3. Open the new bookmarks file and overwrite the checksum value with the one you first copied
  4. Force-close/kill the third-party Chrome browser so it will reload its bookmarks

This method of editing your bookmarks has existed for years as a means to restore your bookmarks on the desktop in case something went awry, but using these steps to get your bookmarks on browsers such as TugaBrowser is a neat application of this old trick. While this method does indeed allow you to get your Chrome bookmarks on your favorite third-party Chrome browser, it requires you to do these steps every single time you add a new bookmark in Chrome. That is certainly not convenient at all, which is why I went about trying to automate this process.

In the video above, notice how when I open TugaBrowser to show off my bookmarks, there are 3 bookmarks shown. When I leave and re-open TugaBrowser, a snackbar is shown telling me that a new bookmark has been detected along with a button to restart TugaBrowser. When I press the button to restart the browser, I open up the bookmarks page and now see a new, fourth bookmark has been added. Essentially, I’ve automated the above 4 steps to work silently in the background, and I’m posting a tutorial here showing you how you can do this too!

Before I start with this tutorial, there are a few things I need to mention:

  • This method requires root access. There is absolutely no way around this requirement, sorry! We’re messing with files in /data/data which can’t be accessed without root.
  • This method is a one-way sync, meaning that any bookmarks you add to your third-party Chrome browser are not saved (and actually are overwritten). You’ll have to add any new bookmarks from within a Chrome browser that has Chrome Sync enabled. I have looked into methods to force Chrome Sync to accept my bookmark changes, but unfortunately I don’t think it’s possible because the version of your bookmarks stored on Google’s server seems to always overrides any changes you manually make.
  • This method is safe. Your bookmarks aren’t in any harm of being erased, because we aren’t (and can’t) mess with Google’s copy of your bookmarks which are stored on their server.

If you are looking for a two-way sync method on your third-party Chrome browser, then sorry, you’ll never get such a method. Blame Google for that. If you are asking yourself “what’s the point of this”, then this tutorial isn’t for you. If you are looking to somewhat ease the pain of Google removing Chrome Sync, then hopefully you’ll find this tutorial useful.


One-Way Chrome Bookmark Sync for Third-Party Chrome Browsers

Requirements:

Although I say Tasker is required, you might be able to get away with using other automation apps on the Play Store. If you do choose to do that, you’re on your own because I have no experience with using them so you’ll have to adapt my script yourself. Synker is necessary because we use it to force a manual refresh of your bookmarks. The snackbar Tasker plugin is technically not necessary, but it is what provides the neat looking snackbar that lets me know there are new bookmarks with a button to restart the app. Finally, you must have Google Chrome (any channel is okay) installed (and not disabled) on your device because it will provide us the bookmarks file we will copy.

One more thing to mention: although my tutorial is built with TugaBrowser in mind, you can easily get this to work with any other Chrome-based browser by modifying a few steps, which I will outline at the end below.

Tutorial

The script I’m using is fairly complex, with 29 total actions, so I won’t be going into excessive detail about how each step works, but just know that this script essentially automates the 4 manual steps outlined in the beginning of the article.

Here’s the Profile description for those of you who are experts at Tasker already and want to try replicating it yourself.

CAF Bookmark Sync

 Profile: CAF Bookmark Sync (28)
Application: TugaBrowser
Enter: Check Chrome Bookmarks (27)
A1: Synker - Force sync [ Configuration:Force sync 2 provider(s) Timeout (Seconds):0 ]
A2: Wait [ MS:0 Seconds:5 Minutes:0 Hours:0 Days:0 ]
A3: Run Shell [ Command:cp /data/data/com.android.chrome/app_chrome/Default/Bookmarks /sdcard/Tasker/Bookmarks Timeout (Seconds):0 Use Root:On Store Output In: Store Errors In: Store Result In: ]
A4: Read File [ File:/sdcard/Tasker/Bookmarks To Var:%json ]
A5: Variable Split [ Name:%json Splitter:"checksum": Delete Base:Off ]
A6: Variable Search Replace [ Variable:%json2 Search:(?<=")[^"]+(?=") Ignore Case:Off Multi-Line:Off One Match Only:On Store Matches In:%checksum Replace Matches:Off Replace With: ]
A7: If [ %ChromeChecksum !Set ]
A8: Variable Set [ Name:%ChromeChecksum To:%checksum(1) Recurse Variables:Off Do Maths:Off Append:Off ]
A9: Else
A10: If [ %ChromeChecksum !~ %checksum(1) ]
A11: Variable Set [ Name:%ChromeChecksum To:%checksum(1) Recurse Variables:Off Do Maths:Off Append:Off ]
A12: Run Shell [ Command:cp /data/data/tugapower.codeaurora.browser/app_chrome/Default/Bookmarks /sdcard/Tasker/TugaBookmarks Timeout (Seconds):0 Use Root:On Store Output In: Store Errors In: Store Result In: ]
A13: Read File [ File:/sdcard/Tasker/TugaBookmarks To Var:%tugajson ]
A14: Variable Split [ Name:%tugajson Splitter:"checksum": Delete Base:Off ]
A15: Variable Search Replace [ Variable:%tugajson2 Search:(?<=")[^"]+(?=") Ignore Case:Off Multi-Line:Off One Match Only:On Store Matches In:%checksum Replace Matches:Off Replace With: ]
A16: Variable Search Replace [ Variable:%json2 Search:(?<=")[^"]+(?=") Ignore Case:Off Multi-Line:Off One Match Only:On Store Matches In: Replace Matches:On Replace With:%checksum(1) ]
A17: Write File [ File:/sdcard/Tasker/Bookmarks Text:%json1"checksum":%json2 Append:Off Add Newline:Off ]
A18: Run Shell [ Command:cp /sdcard/Tasker/Bookmarks /data/data/tugapower.codeaurora.browser/app_chrome/Default/Bookmarks Timeout (Seconds):0 Use Root:On Store Output In: Store Errors In: Store Result In: ]
A19: Snackbar [ Configuration:Message: New bookmarks detected.
Button: RESTART
Command: Timeout (Seconds):15 ]
A20: If [ %sb_button ~ Button Pressed ]
A21: Go Home [ Page:0 ]
A22: Wait [ MS:0 Seconds:2 Minutes:0 Hours:0 Days:0 ]
A23: Kill App [ App:TugaBrowser Use Root:Off ]
A24: Wait [ MS:0 Seconds:2 Minutes:0 Hours:0 Days:0 ]
A25: Launch App [ App:TugaBrowser Data: Exclude From Recent Apps:Off Always Start New Copy:Off ]
A26: End If
A27: Delete File [ File:/sdcard/Tasker/TugaBookmarks Shred Level:0 Use Root:Off ]
A28: End If
A29: End If
A30: Delete File [ File:/sdcard/Tasker/Bookmarks Shred Level:0 Use Root:Off ]

The script activates whenever Tasker detects that you have entered your third-party Chrome browser of choice, in my case TugaBrowser. You will need to enable Tasker’s Accessibility Service so Tasker can detect when you are in your browser of choice. This part can easily be modified to work with other Chrome-based browsers, all you need to do is select which browser you want this script to run on in the Application Context in Tasker.

Now here’s a brief description of what each set of actions in this Task are accomplishing.

  • A1-A2: manually sync Chrome bookmarks from Google’s server so the installed Chrome app’s bookmarks file gets updated. Wait 5 seconds to ensure there’s enough time for the sync to complete
  • A3-A6: copy Chrome’s bookmark file to a temporary location, extract the JSON from the file into a variable, then use a regex filter to extract the checksum value into another variable
  • A7-A10: if Tasker does not have a global variable %ChromeChecksum set (ie. your first time running the script), set it to the current value. If it does have a value set, next check if the value stored in the Tasker variable matches the current checksum in the bookmark file. If so, move on to A11-A26
  • A11: set the Tasker variable holding the Chrome checksum to the current checksum extracted from the bookmark file
  • A12-A15: copy TugaBrowser’s bookmark file to a temporary location, extract the JSON from the file, then use a regex filter to extract the checksum value
  • A16: use the checksum value taken from TugaBrowser and use it to replace the checksum value from Chrome’s bookmark file
  • A17A18: push the updated bookmark file taken from Chrome but with TugaBrowser’s checksum value into TugaBrowser’s data directory
  • A19-A26: show a snackbar telling us new bookmarks have been added. If the button on the snackbar is pressed, restart TugaBrowser, otherwise move on.
  • A27-A30: delete the temporary bookmark files we were working with, and end the Task

Why does this involve so many steps? Unfortunately it’s because we don’t have an easy way to directly access the (potentially large) JSON data structure in the bookmark files through Tasker or one of its plug-in such as AutoTools without copying the file and extracting its contents into a variable. If we could, this work would be much more condensed, but for now this is what I’ve come up with. I have spoken with the developer of AutoTools about this matter, and while he was able to update AutoTools to be able to read JSON data from a file, writing JSON straight to a file couldn’t be done through the Tasker plugin.

In any case, this script itself runs extremely quickly despite some steps seeming unnecessary, because I wanted to get this out there without you needing to install any unnecessary additional plugins on top of Tasker. The only things that slows this script down, and why you see some delay in the video, is the wait time necessary for Tasker to ensure that your Chrome bookmarks have been synced from Google’s servers before moving on with the rest of the script. You can certainly play around with the ‘wait’ commands in the Tasker Task to lower the delay if you would like, but that’s ultimately up to you.


Download, Import, and Setup

As always, we will provide the Tasker Profile XML file that you can grab to quickly get this set up for yourself. Download the .prf.xml file below from AndroidFileHost and save it to your internal storage. Open up Tasker and disable Beginner Mode in Preferences. Go back to Tasker’s main screen and long-press on the “Profile” tab in the action bar up top until you see an “import” option pop up. Press that and then navigate to where you saved the XML file and select it to import it.

Download the “Chrome Bookmark Sync” Profile from AndroidFileHost

There are 3 very, very important steps (and 1 optional, but necessary step if you are not running TugaBrowser) you need to take before this Profile will work for you.

  1. Enable Tasker’s Accessibility Service. You can do so by opening Settings and searching for “accessibility.” Tap on Tasker then enable its Accessibility Service. This is necessary because otherwise Tasker cannot detect when you are using TugaBrowser (or any other browser).
  2. Choose your Chrome Sync providers in Synker. You can do this by opening the “Check Chrome Bookmarks” Task then tapping on Action #1 labeled “Synker – Force sync.” Press the pencil icon to bring up Synker’s configuration screen. Scroll down and select “Chrome Sync” for your Google account.
  3. Grant Tasker superuser access. The quickest way to do this is to make Tasker attempt to perform an Action that requires su. Again inside “Check Chrome Bookmarks” Task configuration screen, this time long-press on Action #3 labeled “Run Shell.” Press the “play” icon that pops up in the bottom left hand corner to make Tasker run this Action, and this one only. Tasker will ask you to grant it superuser access. Note to those of you using MagiskSU: Tasker does not currently detect MagiskSU on its own, so it may throw an error saying your device is not rooted. This will be fixed in the next Tasker update, but if you’re looking for a temporary workaround, XDA Senior Member RandomPooka has a short guide for that.

After you’ve done those 3 things, this Profile should now start working. On your first launch of TugaBrowser/your browser of choice after setting this up, Tasker will save the checksum value of Chrome’s bookmark file. When that checksum value changes in a subsequent launch of TugaBrowser/your browser of choice, then Tasker will replace your browser’s bookmark file with the one from Chrome.

Note that for the first launch of your browser, I intentionally did not set it up so Tasker will copy over Chrome’s bookmark value until it detects a change. This means that your TugaBrowser/browser of choice’s bookmarks won’t change until you make a change in Chrome’s bookmarks. I did this so you will be able to access TugaBrowser/your browser of choice to save your unsynced/offline bookmarks not stored on Google’s servers so my Tasker Profile doesn’t wipe them out without you having a chance to save them elsewhere.


How to Sync Bookmarks on Browsers other than TugaBrowser

In case you aren’t using TugaBrowser, you will need to modify a few steps in order to get this working on your Chrome-based browser of choice. Fortunately, this is very, very easy to do. I will show you how to do so using the open-source Chromium as an example. Here is the list of things you’ll need to change:

  1. Change the Application Context. Rather than “TugaBrowser” being set as the Application Context, change the Application Context to trigger when using the browser of choice. Just tap on the Context and look for your app in the list.
  2. Modify Action #12 (Run Shell command) to point to your particular browser’s bookmark file. The directory should look like /data/data/http://bit.ly/2p2BM9e. (Note: the variables and file names that save it/refer to it as “TugaBookmarks” can safely be ignored, you just need to change where Tasker is pulling the Bookmark file from)
    Before:
    cp /data/data/tugapower.codeaurora.browser/app_chrome/Default/Bookmarks /sdcard/Tasker/TugaBookmarks
    

    After:

    cp /data/data/org.chromium.chrome/app_chrome/Default/Bookmarks /sdcard/Tasker/TugaBookmarks
    
  3. Modify Action #18 (another Run Shell command) to save the updated bookmark file in your particular browser’s data directory. Again, the directory should look like the same as above.
    Before:
    cp /sdcard/Tasker/Bookmarks /data/data/tugapower.codeaurora.browser/app_chrome/Default/Bookmarks
    

    After:

    cp /sdcard/Tasker/Bookmarks /data/data/org.chromium.chrome/app_chrome/Default/Bookmarks 
    
  4. Modify Action #23 to kill your browser of choice. This is so when you press the restart button in the snackbar, Tasker will kill the right application.
  5. Modify Action #25 to re-launch that same browser of your choice. This will re-launch the browser after it was killed by the user so it can load the new bookmark(s).

Everything else can be safely left alone, even if the variable/file names created don’t make sense with your particular browser. You can opt to change them if they bother you, but make sure you know what you’re doing before attempting to do so. Having some Tasker familiarity will definitely help there.


Conclusion

I hope you find this Profile useful. I know there’s a lot to take in here, but it’s really important for you to read through this post carefully so you understand what this does, how it works, and when it will work. This took me a lot of trial and error in getting this right, but I’m pretty happy with the results, even if it seems overly complicated in the end.

Automating this process helps alleviate some of the burden of syncing your bookmarks with Chrome Sync, though unfortunately it will only ever be a one-way sync and will always require root access until Google eases up on its restrictions preventing third-party Chrome browsers from accessing Chrome Sync.

If you have any questions, comments, or concerns, let us know in the comments below!

No comments:

Post a Comment