Skip to content

Instantly share code, notes, and snippets.

@aminnj
Created November 21, 2023 01:15
Show Gist options
  • Save aminnj/d5c9265b3d03176421b8b7736cc197cc to your computer and use it in GitHub Desktop.
Save aminnj/d5c9265b3d03176421b8b7736cc197cc to your computer and use it in GitHub Desktop.
Get page source with running chrome instance in MacOS
import sys
import time
import objc
from ScriptingBridge import SBApplication
import Foundation
import AppKit
# Hide python rocket ship from popping up in Dock when run.
import AppKit
info = AppKit.NSBundle.mainBundle().infoDictionary()
info['CFBundleIconFile'] = u'PythonApplet.icns'
info['LSUIElement'] = True
chrome = SBApplication.applicationWithBundleIdentifier_("com.google.Chrome")
if chrome.isRunning():
windows = chrome.windows()
frontmost_window = chrome.windows()[0] if chrome.windows() else None
tab = frontmost_window.activeTab()
# tab.setURL_("https://news.ycombinator.com/news")
tab.setURL_("view-source:https://news.ycombinator.com/news")
time.sleep(0.5)
tab.viewSource()
time.sleep(0.5)
tab.selectAll()
time.sleep(0.5)
tab.copySelection()
time.sleep(0.5)
tab.close()
# Get the general pasteboard
pasteboard = AppKit.NSPasteboard.generalPasteboard()
# Get the content of the clipboard
clipboard_content = pasteboard.stringForType_(AppKit.NSStringPboardType)
print(clipboard_content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment