Building a custom Google Chrome extension

19 Dec 2022

Goal would be to create an extension that triggers a Python script Clipee, passing the current URL & page content as arguments.

Different logic would be applied depending on the URL, or based on a different parameter.

Extension to be triggered by a keyboard shortcut.

This will allow to collect data from the web much quicker & better than collecting links and scraping later.

ChatGPT provided the following guidance:

chrome-extension/221219-2227-chatgpt-chrome-extension.jpg

chrome-extension/221219-2247-chatgpt-chrome-extension.jpg

Resources

20 Dec 2022

This suggestion from ChatGPT could be interesting: triggering a shell script for every URL change.

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.url) {
    // If the URL of the tab has changed, trigger the shell script with the new URL as a parameter
    var scriptPath = '/path/to/your/shell/script';
    var url = changeInfo.url;
    chrome.runtime.sendNativeMessage(
      'com.example.native_host', 
      { script: scriptPath, url: url },
      function(response) {
        console.log('Received response from native host: ', response);
      }
    );
  }
});

chrome-extension/221220-2315-chatgpt-chrome-extension.jpg

An idea could be to implement this with logic to kick in only if a specific URL pattern is detected (eg 'linkedin.com/in/').
The shell script can trigger a Python script to save the page as HTML file in a dedicated folder.
This would allow to automatically save every HTML page from LinkedIn profiles visited, without doing anything.

Working extension

05 Jan 2023

Got some help and finally made it work.

This extension enables to save a HTML file in a given folder for any Linkedin profile visited:

manifest.json

{
    "name": "Save Linkedin Profiles",
    "version": "3.0",
    "manifest_version": 3,
    "description": "saves automatically Linkedin profiles visited to given folder as HTML file",
    "icons": { 
        "16": "icon.png",
        "32": "icon.png",
        "48": "icon.png",
        "128": "icon.png"
},

    "background": {
        "service_worker": "background.js"
},

    "host_permissions": ["<all_urls>"],


"permissions":[
    "downloads",
    "scripting",
    "tabs"
]
}

background.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if(request.message=="download"){
html = request.html;
var dataUrl = 'data:text/html,' + encodeURIComponent(html);
filename = new URL(request.urlo)
filename = filename.href.replace(filename.origin,"").replace(/[^a-zA-Z0-9]/g, '_');
chrome.downloads.download({
    'url': dataUrl,
    'filename': "Linkedin/profiles/"+filename+".html",
    'saveAs': false
});
}
}) 

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (tab.status == 'complete' && tab.url.search("chrome://")<0) {

urlo  = tab.url
if(urlo.search("linkedin.com/in/")>=0){
    chrome.scripting.executeScript({ target: { tabId }, func:runner,args:[urlo]});
}
}

function runner(urlo){
    if(typeof(exist)=="undefined"){
    chrome.runtime.sendMessage({"message":"download","html":document.body.innerHTML,urlo:urlo})
exist = true
}
} 
});

To implement this extension, have these files in a folder.
Then in Chrome:

  • go to chrome://extensions
  • activate Developer mode
  • click on Load unpacked
  • select the folder containing the files

It will be automatically triggered when visiting a Linkedin profile.

I have duplicated this extension to also save Linkedin Company profiles to a separate folder.

links

social