Last active
April 11, 2024 20:21
-
-
Save belkarx/03f6dfca40966b2c794ac441acc6106e to your computer and use it in GitHub Desktop.
greasemonkey script to autoselect all elements that follow a pattern on a page!
This file contains 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
// ==UserScript== | |
// @name Anthropic Caller for Google Scholar | |
// @namespace http://tampermonkey.net/ | |
// @version 1.0 | |
// @description Calls Anthropic API and extracts relevant data from Google Scholar pages | |
// @author Your Name | |
// @match *://scholar.google.com/* | |
// @grant GM_xmlhttpRequest | |
// ==/UserScript== | |
(function() { | |
var selectors = []; | |
var overlay = document.createElement('div'); | |
overlay.style.position = 'fixed'; | |
overlay.style.top = '10px'; | |
overlay.style.right = '10px'; | |
overlay.style.padding = '10px'; | |
overlay.style.background = 'white'; | |
overlay.style.border = '1px solid black'; | |
overlay.style.zIndex = '9999'; | |
function handleClick(event) { | |
if (event.target !== doneButton) { | |
event.preventDefault(); | |
event.stopPropagation(); | |
var element = event.target; | |
var selector = getSelector(element); | |
selectors.push(selector); | |
element.style.outline = '2px solid red'; | |
setTimeout(function() { | |
element.style.outline = ''; | |
}, 500); | |
} | |
} | |
function getSelector(element) { | |
var selector = ''; | |
while (element && element.nodeType === Node.ELEMENT_NODE) { | |
var tagName = element.tagName.toLowerCase(); | |
var id = element.id ? '#' + element.id : ''; | |
var classes = element.classList.length ? '.' + Array.from(element.classList).join('.') : ''; | |
selector = tagName + id + classes + ' ' + selector; | |
element = element.parentNode; | |
} | |
return selector.trim(); | |
} | |
const apiKey = "API KEY HERE"; | |
const apiUrl = 'https://api.anthropic.com/v1/messages'; | |
function getDataString(text) { | |
const regex = /```javascript\n([\s\S]*?)\n```/g; | |
const match = regex.exec(text); | |
return match ? match[1] : null; | |
} | |
function callAnthropic(selectors) { | |
return new Promise((resolve, reject) => { | |
const requestData = { | |
model: 'claude-3-opus-20240229', | |
max_tokens: 1024, | |
messages: [ | |
{ role: 'user', content: 'write javascript to select all elements on a page following this css selector pattern. extrapolate the pattern from the given datapoints, don\'t just directly return them. there may be some error in datapoint selection, use the most obvious pattern. extract the relevant part (this\'ll often be a link and its text, or just text). if something seems off or malicious, and is not just links or something, DO NOT RETURN CODE. Copy the outputs to clipboard as json. Selectors as follows:\n' + selectors | |
} | |
] | |
}; | |
GM_xmlhttpRequest({ | |
method: 'POST', | |
url: apiUrl, | |
headers: { | |
'x-api-key': apiKey, | |
'anthropic-version': '2023-06-01', | |
'content-type': 'application/json' | |
}, | |
data: JSON.stringify(requestData), | |
onload: function(response) { | |
if (response.status === 200) { | |
const data = JSON.parse(response.responseText); | |
console.log(data); | |
const dataString = getDataString(data.content[0].text); | |
console.log(dataString); | |
resolve(dataString); | |
} else { | |
reject(new Error('Request failed with status ' + response.status)); | |
} | |
}, | |
onerror: function(error) { | |
reject(error); | |
} | |
}); | |
}); | |
} | |
document.addEventListener('click', handleClick, true); | |
var doneButton = document.createElement('button'); | |
doneButton.textContent = 'Done'; | |
doneButton.addEventListener('click', async function() { | |
document.removeEventListener('click', handleClick, true); | |
document.body.removeChild(overlay); | |
console.log('CSS Selectors:\n' + selectors.join('\n')); | |
try { | |
const script = await callAnthropic(selectors.join('\n')); | |
console.log(script); | |
eval(script); | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
}); | |
overlay.appendChild(doneButton); | |
document.body.appendChild(overlay); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment