Last active
May 6, 2023 00:09
-
-
Save DinisCruz/f9a3a3a0ffae8285c530 to your computer and use it in GitHub Desktop.
Misc Chrome extensions code snippets (to add to helper doc)
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
url = 'http://127.0.0.1:4444/wd/hub/sessions' | |
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
console.log(tabId) | |
chrome.tabs.update(tabId, {url: url}) | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = "text=$('body').text();" + | |
"json=JSON.stringify(JSON.parse(text),null,' ');" + | |
"$('body').html('<pre class=prettyprint>' + json + '</pre>');" + | |
"$.getScript('https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js');" | |
chrome.tabs.executeScript(tabId, { code: code }) | |
}); | |
}); | |
}); |
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
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = "text=$('body').text();" + | |
"json=JSON.stringify(JSON.parse(text),null,' ');" + | |
"$('body').html('<pre class=prettyprint>' + json + '</pre>');" + | |
"$.getScript('https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js');" | |
chrome.tabs.executeScript(tabId, { code: code }, function(data) | |
{ | |
console.log(data) | |
}) | |
}); | |
}); |
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
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = "text=$('body').text();" + | |
"json=JSON.stringify(JSON.parse(text),null,' ');" + | |
"$('body').html('<pre>' + json + '</pre>')"; | |
chrome.tabs.executeScript(tabId, { code: code }, function(data) | |
{ | |
console.log(data) | |
}) | |
}); | |
}); |
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
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = "$('body').text()"; | |
chrome.tabs.executeScript(tabId, { code: code }, function(data) | |
{ | |
console.log(data) | |
}) | |
}); | |
}); |
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
options = { | |
active:true, | |
windowType:"normal", | |
currentWindow: true | |
} | |
chrome.tabs.query(options,function(d) | |
{ | |
console.log(JSON.stringify(d, null, ' ')); | |
}); | |
// or | |
options = { | |
active:true, | |
windowType:"normal", | |
currentWindow: true | |
} | |
chrome.tabs.query(options,function(tabs) | |
{ | |
console.log('current tab is: ' + tabs[0].id); | |
}) |
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
//set and get an value | |
chrome.storage.local.set({test:'this is a test'}, | |
function() | |
{ | |
chrome.storage.local.get('test',function(data) { console.log(data.test) } ) | |
}) | |
//set another value and get all stored data | |
chrome.storage.local.set({test2:'more data'}, | |
function() | |
{ | |
chrome.storage.local.get(null,function(data) { console.log(JSON.stringify(data)) } ) | |
}) |
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
//onBeforeRequest | |
chrome.webRequest.onBeforeRequest.addListener( | |
function(info) { | |
console.log("request intercepted: " + info.url); | |
return {cancel:false} | |
},{urls: ["<all_urls>"]}) | |
//onCompleted | |
chrome.webRequest.onCompleted.addListener( | |
function(details) { | |
if (details.url.substring(0, 23) == "https://www.google.com/") // I know I do not need this | |
{ | |
console.info("URL :" + details.url); | |
} | |
}, | |
// filters | |
{ | |
urls: [ | |
"http://*.google.com/*", | |
"https://*.google.com/*", | |
], | |
types: ["image"] | |
}, | |
["responseHeaders"]); |
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
if (chrome.webRequest.onBeforeRequest.hasListeners() == false) | |
{ | |
listener = function(info) | |
{ | |
console.log("request intercepted: " + info.url); | |
return {cancel:false} | |
} | |
chrome.webRequest.onBeforeRequest.addListener(listener, {urls: ["<all_urls>"]}) | |
console.log('ADDED onBeforeRequest hook') | |
} | |
else | |
{ | |
console.log('REMOVED onBeforeRequest hook') | |
chrome.webRequest.onBeforeRequest.removeListener(listener) | |
} |
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
//simple | |
chrome.tabs.getSelected(null, function (tab) { | |
var video_constraints = { | |
mandatory: { | |
chromeMediaSource: 'tab' | |
} | |
}; | |
var constraints = { | |
audio: false, | |
video: true, | |
videoConstraints: video_constraints | |
}; | |
chrome.tabCapture.capture(constraints, function (stream) { | |
console.log('got stream') | |
console.log(stream) | |
// it is a LocalMediaStream object!! | |
}); | |
}); | |
//more options | |
chrome.tabs.getSelected(null, function(tab) { | |
var MediaStreamConstraint = { | |
audio: false, | |
video: true, | |
videoConstraints: { | |
mandatory: { | |
chromeMediaSource: 'tab', | |
minWidth: 1280, | |
minHeight: 720, | |
maxWidth: 1920, | |
maxHeight: 1080, | |
minAspectRatio: 1.77 | |
} | |
} | |
}; | |
function callback(stream) { | |
if (!stream) { | |
console.error('Unable to capture the tab. Note that Chrome internal pages cannot be captured.'); | |
return; | |
} | |
console.log ('in callback') | |
console.log(stream) | |
} | |
chrome.tabCapture.capture(MediaStreamConstraint, callback); | |
}); | |
// get captured tabs | |
chrome.tabCapture.getCapturedTabs(function(tabs) { console.log(tabs); _tabs = tabs } ) | |
//this return | |
// blob:chrome-extension%3A//mmkohkjfbpoeheiknjdpfbchbdodmmje/d92c1dd3-bac0-4c57-b93f-aa4a660d9542 | |
// [19:11:35]: [object MediaStream] | |
// next step is to figure out how to use it | |
chrome.tabs.getSelected(null, function (tab) { | |
var video_constraints = { | |
mandatory: { | |
chromeMediaSource: 'tab' | |
} | |
}; | |
var constraints = { | |
audio: false, | |
video: true, | |
videoConstraints: video_constraints | |
}; | |
chrome.tabCapture.capture(constraints, function (stream) { | |
console.log('got stream') | |
_stream = stream | |
console.log(stream) | |
var src = URL.createObjectURL(stream); | |
console.log(src) | |
}); | |
}) |
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
chrome.tabs.getSelected(null, function (tab) | |
{ | |
var video_constraints = { | |
mandatory: { | |
chromeMediaSource: 'tab' | |
} | |
}; | |
var constraints = { | |
audio: false, | |
video: true, | |
videoConstraints: video_constraints | |
}; | |
chrome.tabCapture.capture(constraints, function (stream) | |
{ | |
console.log('got stream') | |
_stream = stream | |
console.log(stream) | |
options = { | |
active:true, | |
windowType:"normal", | |
currentWindow: true} | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
console.log('adding video: ' + _stream) | |
$('body').append('<video style="position:absolute;top:0px;z-index:1000" />') | |
videoElement = $('video')[0] | |
console.log(videoElement) | |
videoElement.src = URL.createObjectURL(_stream); | |
console.log(src) | |
videoElement.play(); | |
console.log('all done') | |
}); | |
}); | |
}); | |
}) |
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
//a) listener | |
chrome.extension.onMessage.addListener(function(data, sender, sendResponse) { | |
console.log("inside chrome.extension.onMessage.addListener") | |
console.log(data) | |
_data = data | |
}) | |
//options = {stream:'goes here'} | |
//chrome.extension.sendMessage(options) | |
//b) send stream | |
chrome.tabs.getSelected(null, function (tab) | |
{ | |
var video_constraints = { | |
mandatory: { | |
chromeMediaSource: 'tab' | |
} | |
}; | |
var constraints = { | |
audio: false, | |
video: true, | |
videoConstraints: video_constraints | |
}; | |
chrome.tabCapture.capture(constraints, function (stream) | |
{ | |
console.log('got stream') | |
_stream = stream | |
console.log(stream) | |
chrome.extension.sendMessage({stream:stream}) | |
}); | |
}) | |
//b) sending URL | |
chrome.extension.onMessage.addListener(function(data, sender, sendResponse) { | |
console.log("inside chrome.extension.onMessage.addListener") | |
console.log(data) | |
_data = data | |
}) | |
//options = {stream:'goes here'} | |
//chrome.extension.sendMessage(options) | |
//b) send stream | |
chrome.tabs.getSelected(null, function (tab) | |
{ | |
var video_constraints = { | |
mandatory: { | |
chromeMediaSource: 'tab' | |
} | |
}; | |
var constraints = { | |
audio: false, | |
video: true, | |
videoConstraints: video_constraints | |
}; | |
chrome.tabCapture.capture(constraints, function (stream) | |
{ | |
console.log('got stream') | |
_stream = stream | |
console.log(stream) | |
url = URL.createObjectURL(stream) | |
console.log(url) | |
chrome.extension.sendMessage({url:url}) | |
}); | |
}) | |
//and | |
crome.extension.onMessage.addListener(function(data, sender, sendResponse) { | |
console.log("inside chrome.extension.onMessage.addListener") | |
console.log(data) | |
_data = data | |
}) | |
videoElement = $('video')[0] | |
videoElement.src = _data.url | |
/*$('body').append('<video style="position:absolute;top:0px;z-index:1000" />') | |
videoElement = $('video')[0] | |
console.log(videoElement) | |
videoElement.src = URL.createObjectURL(_data.stream); | |
console.log(src) | |
videoElement.play();*/ | |
//options = {stream:'goes here'} | |
//chrome.extension.sendMessage(options) | |
//we get |
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
//when running as main page | |
$('topbanner').hide() | |
if($('#target').length ==0) | |
console.log('creating iframe') | |
$('body').append('<iframe id="target">') | |
target = $('#target') | |
target.css({position:'absolute' , top:'0px', width:'500px', height:'400px', right:'0px'}) | |
target.attr('src','chrome-extension://mmkohkjfbpoeheiknjdpfbchbdodmmje/options.html') | |
return target.length; | |
//when running in popup | |
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
console.log(tabId) | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = ""+ | |
"if($('#target').length ==0) " + | |
"{ " + | |
" console.log('creating iframe');" + | |
" $('body').append('<iframe id=target >'); " + | |
"}" + | |
"target = $('#target');" + | |
"target.css({position:'absolute' , top:'0px', width:'500px', height:'400px', right:'0px'});"+ | |
"target.attr('src','chrome-extension://mmkohkjfbpoeheiknjdpfbchbdodmmje/options.html');" + | |
""; | |
chrome.tabs.executeScript(tabId, { code: code }) | |
console.log('all done') | |
}); | |
}); |
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
options = { active:true, windowType:"normal", currentWindow: true } | |
chrome.tabs.query(options,function(tabs) | |
{ | |
tabId = tabs[0].id | |
chrome.tabs.executeScript(tabId, {file:'bower_components/jquery/dist/jquery.min.js'}, function() | |
{ | |
code = "$('a').html('abcedf')"; | |
chrome.tabs.executeScript(tabId, { code: code }) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment