-
-
Save zeeshanrasool91/8997c08892e5c3c2c5ef403dfdc884a5 to your computer and use it in GitHub Desktop.
Get the URL of the current page in the Google Chrome browser on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // For Android | |
| // Get the URL of the current page in the Google Chrome browser | |
| // Needs to be in a class that extends AccessibilityService | |
| // A version of this code is used in PorNo!, a porn-blocker I wrote | |
| // https://github.com/mrvivacious/PorNo-_Porn_Blocker/ | |
| // | |
| // Have fun ~ | |
| String TAG = "Get Chrome URL: "; | |
| // https://stackoverflow.com/questions/38783205/android-read-google-chrome-url-using-accessibility-service | |
| // https://web.archive.org/web/20161228170758/http://axovel.com/blog/2016/05/06/taking-accessibility-service-to-next-level-android/ | |
| @Override | |
| public void onAccessibilityEvent(AccessibilityEvent event) { | |
| long start = System.currentTimeMillis(); | |
| // Chrome, search other browsers with their package name | |
| if (event.getPackageName() != null && event.getPackageName().toString().contains("com.android.chrome")) { | |
| AccessibilityNodeInfo source = event.getSource(); | |
| if (source == null) { | |
| return; | |
| } | |
| // Id found at https://github.com/chromium/chromium/blob/master/chrome/android/java/res/layout/url_bar.xml ♥ | |
| List<AccessibilityNodeInfo> findAccessibilityNodeInfosByViewId = | |
| source.findAccessibilityNodeInfosByViewId("com.android.chrome:id/url_bar"); | |
| Log.d(TAG,"List: " + findAccessibilityNodeInfosByViewId); | |
| if (findAccessibilityNodeInfosByViewId.size() > 0) { | |
| AccessibilityNodeInfo omniboxView = findAccessibilityNodeInfosByViewId.get(0); | |
| // Uncomment to see a trace from node to source event | |
| // traceParent(omniboxView); | |
| String currentURL = omniboxView.getText().toString(); | |
| Log.d(TAG, currentURL); | |
| Log.d(TAG, "Time for retreival (ms) = " + (System.currentTimeMillis() - start)); | |
| } | |
| } | |
| } | |
| // If you want to see how this node was found | |
| private void traceParent(AccessibilityNodeInfo omniboxView) { | |
| // Get id of current node | |
| // Get parent node, which has children | |
| // for children in parent node children: if child.id = current id : print iteration | |
| int level = 0; | |
| while (omniboxView.getParent() != null) { | |
| int currentId = omniboxView.getWindowId(); | |
| AccessibilityNodeInfo parentNode = omniboxView.getParent(); | |
| for (int i = 0; i < parentNode.getChildCount(); i++) { | |
| AccessibilityNodeInfo child = parentNode.getChild(i); | |
| Log.d(TAG, "LEVEL : Child number " + level + " : " + i); | |
| Log.d(TAG, "Child " + child.toString()); | |
| if (child.getWindowId() == currentId) { | |
| Log.d(TAG, "LEVEL : Child number " + level + " : " + i); | |
| Log.d(TAG, "LEVEL : Child number " + currentId + " : " + child.getWindowId()); | |
| break; | |
| } | |
| } | |
| Log.d(TAG, "LEVEL " + level + ": " + omniboxView.getParent().toString()); | |
| omniboxView = omniboxView.getParent(); | |
| level++; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment