I work on a little extension on Google Chrome, I want to create a new tab, go on the url "sample"+i+".com", launch a content script on this url, update the current tab to "sample"+(i+1)+".com", and launch the same script. I looked the Q&A available on stackoverflow and I google it but I didn't found a solution who works. This is my actually code of background.js (it works), it creates two tabs (i=21 and i=22) and load my content script for each url, when I tried to do a chrome.tabs.update Chrome launchs directly a tab with i = 22 (and the script works only one time) :
function extraction(tab) {
for (var i =21; i<23;i++)
{
chrome.storage.sync.set({'extraction' : 1}, function() {}); //for my content script
chrome.tabs.create({url: "http://example.com/"+i+".html"}, function() {});
}
}
chrome.browserAction.onClicked.addListener(function(tab) {extraction(tab);});
If anyone can help me, the content script and manifest.json are not the problem. I want to make that 15000 times so I can't do otherwise.
Thank you.
I guess chrome.tabs.create is an async function so you need to create a separate function so that the i variable is copied each time:
try this:
var func = function(i)
{
chrome.storage.sync.set({'extraction' : 1}, function() {}); //for my content script
chrome.tabs.create({url: "http://example.com/"+i+".html"}, function() {});
}
function extraction(tab) {
for (var i =21; i<23;i++)
{
func(i);
}
}
chrome.browserAction.onClicked.addListener(function(tab) {extraction(tab);});
You need to make sure that the tab finished loading, and that your content script finished running, before updating the tab to the next url. One way to achieve that would be by sending a message from the content script to the background page. You can include the following in your content script:
chrome.extension.sendMessage("finished");
In your background script you can do the following:
var current = 21, end = 23;
chrome.extension.onMessage.addListener(
function(request, sender) {
if( request == "finished" && current <= end ) {
chrome.tabs.update( sender.tab.id,
{url: "http://example.com/"+current+".html"});
current++;
}
}
);
chrome.tabs.create({url: "http://example.com/"+current+".html"});
Related
I want x to increase each time a new tab opens. Why isn't it working?
var x = 0;
function increase(){
x++;
}
chrome.tabs.onCreated.addListener(function (tab) {
increase();
});
Your issue is that the tabs api cannot be used within a content script (check out the docs here for what can and can't).
In order to achieve what your trying to do, you need to place your tab counting code in the background script. If you need access to the variable x within your content script you'll have to pass the data between your background and content scripts using message passing.
For example you could setup your background script to increase x whenever a new tab is opened, then whenever your content script needed this value it could ask the background script for it:
Background.js:
var x = 0;
function increase(){
x++;
}
chrome.tabs.onCreated.addListener(function (tab) {
increase();
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action == "tabCount")
sendResponse({count: x});
});
Content-Script.js:
chrome.runtime.sendMessage({action: "tabCount"}, function(response) {
console.log(response.count);
});
I am using proxy.pac file to route my traffic in chrome browser.Whenever I change my proxy.pac file I need to manually click reapply settings button here chrome://net-internals/#proxy to make it work. My proxy.pac file will change frequently so it is difficult to manually apply changes every time.Is there a way to automate this process like any add ons or scripts.
Foxyproxy add on has a option to reload pac file automatically in Firefox but in chrome that option is not provided.
I just Modified your code based on Pac-Script proxy setting. It means it works only if you have proxy mode pacscript thats it.
document.addEventListener("DOMContentLoaded", function () {
var errorHandler = new ProxyErrorHandler();
var persistedSettings = ProxyFormController.getPersistedSettings();
if (persistedSettings !== null) {
if (persistedSettings.regular.mode == 'pac_script') {
// Do something every 5 seconds
setInterval(function() {
// call URL with random string to avoid URL cache
persistedSettings.regular.pacScript.url = 'myfile/path/for/fun.pac?nocache'+Math.floor((Math.random() * 1000) + 1);
chrome.proxy.settings.set({'value': persistedSettings.regular});
}, 5000);
}
chrome.proxy.settings.set({'value': persistedSettings.regular});
}
});
Download this sample chrome extension https://developer.chrome.com/extensions/examples/extensions/proxy_configuration.zip and replace the background.js with below script.Load this extension in developer mode.
document.addEventListener("DOMContentLoaded", function () {
var errorHandler = new ProxyErrorHandler();
var persistedSettings = ProxyFormController.getPersistedSettings();
setInterval(function() {
// Do something every 5 seconds
if (persistedSettings !== null) {
chrome.proxy.settings.set({'value': 'myfile/path/for/fun.pac'});
}
}, 5000);
});
This will reload PAC file every 5 seconds.
Hi I have try to realize that...but I have some problem with CKeditor control:
autosave function in Ajax mode
With Firebug I see the POST sending for a simple field (text for example) but the post of CKEDITOR is not correct (I see only the initial value when I open the XPages)
have anyone any idea?
P.S. I have add this code into the onstart function:
for(var instanceName in CKEDITOR.instances) {
CKEDITOR.instances[instanceName].updateElement();
}
Now I see the POST with correct HTML...don't seem work
Ok I have resolve the problematic I have insert this native RichText code in top of my XPages:
function CKEDITOResubmit(idCKEDITOR){
var rte=dijit.byId(idCKEDITOR);
var txta=XSP.getElementById(idCKEDITOR+'_h');
if(!rte || !txta) return;
txta.value = rte.getValue();
var mod=XSP.getElementById(idCKEDITOR+'_mod');
mod.value=rte.isModified(txta.value);
return true;
}
When start automatic routine of Update:
executeOnServer('autoSaveDoc',null,
{'valmode': 1,
onStart:function() {
for(var instanceName in CKEDITOR.instances) {
CKEDITOResubmit(instanceName)
}
btn.innerHTML="saving....";console.log("autosave start"); },
onComplete:function() {btn.innerHTML="saved!"; console.log("autosave complete")},
onError: function() {btn.innerHTML="error saving"; console.log("autosave error") }
})
I have a popup, call 'welcome.html', the thing I would like to do is when the user select a text, and click my plugin, it will use some of the page information, and print back to the welcome.html. For example, the web site title, and the text which the user selected and the url. But how can I pass value to that welcome.html? Thank you.
I do a lot of this in my extension as it mines a lot of data enabling the user to easily copy it to their clipboard.
Since you're looking for a lot less data it's even simpler. When your popup is being loaded you can call the following function to retrieve the information you require;
function getData(callback) {
chrome.tabs.getSelected(null, function (tab) {
var data = {
selection: '',
title: tab.title,
url: tab.url
};
/*
* We can't call content scripts on some pages and the process will get
* stuck if we try.
*/
if (tab.url.indexOf('chrome') === 0 ||
tab.url.indexOf('https://chrome.google.com/webstore') === 0) {
callback(data);
} else {
chrome.tabs.sendRequest(tab.id, {}, function (response) {
data.selection = response.selection;
callback(data);
});
}
});
}
Ensure you pass in a callback function which will be called once all the data has been extracted;
getData(function (data) {
console.log('Title: ' + data.title);
console.log('URL: ' + data.url);
console.log('Selected Text: ' + data.selection);
// Display the data instead
});
As you may have noticed the getData function sends a request to the selected tab. A content script will need to be injected in to the page in order for this to work so be sure you've configured your manifest correctly or injected it programmatically prior to calling getData or it won't work. The script that will need to be injected should resemble the following;
(function () {
chrome.extension.onRequest.addListener(function (request, sender,
sendResponse) {
sendResponse({
selection: window.getSelection().toString()
});
});
})();
This simply returns the currently selected text. One concern is that this data look-up could potentially cause a slight pause while the popup is rendered but you should test this yourself and experiment with it but there are solutions.
This should cover all you need to know so good luck and let me know if you need any further help as I'm aware this could be slightly overwhelming if you're new to Chrome extensions.
I'm not good at JS and I'm having some -I hope- stupid problem I'm not seeing on my code... if you guys could help me out, I'd really appreciate it.
My extension does some stuff with the current tab's URL. It worked ok using the onUpdate event on my background page, setting the tab's URL on a variable and then I used it on a pop-up.
The thing is that if the user starts, selecting different tabs, without updating the URLs my event won't be triggered again... so I'm now also listening to the onSelectionChanged event.
The thing is that there's no "tab" object within the onSelectionChanged event's parameters, so I cannot ask for the tab.url property.
I tried to use the chrome.tabs.getCurrent() method, but obviously I'm doing something wrong... and I reached the limit of my -very little- knowledge.
Here's the code, if you guys could take a look and point me in the right direction, I'll really appreciate it.
<script>
var tabURL = '';
var defaultURLRecognition = [ "test" ];
// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
//THIS IS WHAT'S NOT WORKING, I SUPPOSE
if (tab==undefined) {
chrome.tabs.getCurrent(function(tabAux) {
test = tabAux;
});
}
//
// If there's no URLRecognition value, I set the default one
if (localStorage["URLRecognition"]==undefined) {
localStorage["URLRecognition"] = defaultURLRecognition;
};
// Look for URLRecognition value within the tab's URL
if (tab.url.indexOf(localStorage["URLRecognition"]) > -1) {
// ... show the page action.
chrome.pageAction.show(tabId);
tabURL = tab.url;
}
};
// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
// Listen for tab selection changes
chrome.tabs.onSelectionChanged.addListener(checkForValidUrl);
</script>
I would do something like this:
function checkForValidUrl(tab) {
//...
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
if(changeInfo.status == "loading") {
checkForValidUrl(tab);
}
});
chrome.tabs.onSelectionChanged.addListener(function(tabId, selectInfo){
chrome.tabs.getSelected(null, function(tab){
checkForValidUrl(tab);
});
});