Difference between executing script on 'changeInfo.status = loading' and 'changeInfo.status = completed' - google-chrome-extension

What difference does it make on executing script when
changeInfo.status = loading vs changeInfo.status == 'completed' in chrome.tabs.onUpdated.addListener?
Because when we execute a script when the status is loading the injected script intercepts fetch calls while it doesn't on status completed.
Any help?
details : we are trying to intercept fetch call of a website through the injected script injected on chrome.tabes.onUpdated. The issue is when injected script is executed when changeInfo.status = complete,inject script cannot intercept fetch calls..but if the injected script is executed on changeInfo.status = loading it intercepts the fetch calls. Why does this happen was my question?

Related

Injected script unable to access chrome.storage API

I am writing a chrome extension, using a content script to inject some javascript code. As follows:
let actualCode = 'My Injected JS Code';
let script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
For the injected script, I would like to use some value stored in chrome.storage.sync. However, I found that the API is unaccessible to the injected script (storage.sync is undefined). The API is only accessible within the content script, but not for the injected script. Any ideas how I could access chrome.storage API for the injected script too?
PS: I've registered the storage API in manifest.json
PS: When I open the developer's console on chrome and type "chrome.storage", it returns undefined too. I think this might be a permission problem?
The inject way you used for the script, made it work in the web page environment, which doesn't have access to most of Chrome Extensions API.
However, there is an option to use Messages API which allows sending requests from a webpage to the extension by ID.
In this case, you also need to implement a listener in your background page, to answer such requests.
P.S. chrome.storage API also shouldn't be available from page console. You may want to debug it from your background page console or select a content script environment (if the extension has such):
you can use window.postMessage({type : "MESSAGE_NAME"}) in injected script to send "message" event. then use windows.addEventListner("message", callback) in content_script to listen on "message" event.
You can specify the type of message to which you want to listen in the callback function. for instance
in content_script.js
function func_callback(){
if (event.data.type && event.data.type === "MESSAGE_NAME") {
# Your code
}`enter code here`
}
windows.addEventListner("message", func_callback)
in injected_script.js
<button onClick={()=>window.postMessage({type : "MESSAGE_NAME"})}></button>

Avoid using cy.wait() to wait for a page to load due to aborted get request cypress

I'm running a cypress test that requires a page to load before clicking an element and logging in, but for some reason a failed (and aborted) GET request is causing the page to take forever to load and eventually it times out unless I add a cy.wait(6000) before the cy.click() call. I need to somehow wait for the page to load without using a cy.wait(). How can I do this, given that I cant fix the aborted GET request?
cy.visit(loginUrl)
cy.url().should('contain', '#/loginPortal')
cy.wait(6000) //Allows page to load before trying to log in, needs to be removed
cy.contains('ButtonText').click()
You can make Cypress wait for any specific XHR call, before you assert. How long it waits is defined by the responseTimeout configuration.
cy.server();
cy.route('**/api/getData').as('getData');
cy.visit('/home');
cy.wait('#getData');
cy.contains('ButtonText').click()
Cypress best practices: Unnecessary-Waiting.
Cypress docs on wait Alias.
It's useful to tell Cypress to wait for a specific URL to be present in the address bar before continuing.
cy.get('#login').click();
cy.location('pathname', {timeout: 20000}).should('include', '/path/to/page');
This can be used to wait for redirects which are triggered after login or any page change. That may be a necessary first step before using cy.wait for an XHR request, as Rui Marques describes.

how to run agents in xpages from a web browser?

I got this problem and started googling about it, but no direct answer were pulled out. My query problem is, I'm doing an xpage project and I need to run an agent that uses lotusscript as a language. The agent is used to read a TSV text file and create notes document from each record there. Independently running the agent went very good, no problem. But when I tried to run it from xpage using this script :
var doc = database.createDocument();
var field = getComponent("filePath");
var agent:NotesAgent = database.getAgent("UploadTSV");
if (agent != null) {
agent.runWithDocumentContext(doc);
TSVDoc.setValue("filePath","Agent run");
}
else{
TSVDoc.setValue("filePath","Agent did not run");
}
it did not run. I'm just wondering what I did wrong. Thank you in advance.
My way to do this would be to trigger the agent (either it's based on a page load event or on a user click event) via client Javascript. The URL to run an agent is nothing more than
http://yourhost/yourapp.nsf/youagent?openagent
So I'd just make a AJAX call to that URL to run the agent. To get return values (errors of anything else) I'd add some code to the agent's print output. Print statements (in Lotusscript) in agents called from the browser produce a HTTP response. Similar for agents written in Java but there you have to do more than simple sysouts.

Access to requestContent within AfterRequest Script soapui

I'm trying to simulate an asynchronous response on a webservice mock. The goal is to response a synchronous acknowledge message and then a delayed message back to the replyTo address. The approach I have selected uses mock service that will handle the acknowledge and then run a test case that will handle the processed message back to the replyTo. I'm using OnRequest Script to generate the acknowledged message and AfterRequest Script to run the test case that will emulate the delay and the response back to the replyTo.
So the question is which script object I can use to have access to the requestContent. I have seen examples using:
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
but the mockRequest instance is not available on AfterRequest, Which object I can use instead to have a holder with the request content?
I did find that
def holder = new com.eviware.soapui.support.XmlHolder( mockResult.getMockRequest().requestContent )
do the trick, but now I find that running a test script in AfterRequest delays the synchronous response back, Why could this be happening? Isn't AfterRequest's script executed after the mock service response back? Do i have to explicitly execute something at Dispatch or at OnRequest in order to summit back the response before AfterRequest code being executed?
I know this is really old question, but I just faced the same issue myself. I have no idea why it works as it works, but you can avoid the problem by accessing the request content in OnRequest, then store needed information to context and use the context in AfterRequest to get the information you need.

Rejoining a chrome.cast.Session in an ajax app?

Our site uses ajax to navigate pages and that's making it hard to request a new session when the page changes. Ideally when a user changes pages, I'd like to stop the currently playing session and start a new session with the video that's on the new page.
On the first pageload, I append the https://www.gstatic.com/cv/js/sender/v1/cast_sender.js script, call the init method:
var sessionRequest = new chrome.cast.SessionRequest(applicationID);
var apiConfig = new chrome.cast.ApiConfig(sessionRequest,
this.sessionListener.bind(this),
this.receiverListener.bind(this));
chrome.cast.initialize(apiConfig, this.onInitSuccess.bind(this), this.onError.bind(this));
everything works fine, my callbacks are called and I can start the chromecast session.
On secondary page loads, I don't re-add the cast_sender.js script. When I call the initalize method and the sessionListener callback doesn't execute. If I try holding on to the session between pages, I can access the session and get to it's media object, but if I try calling any methods on the session or media object, I just get back an error:
TypeError: Cannot call method 'postMessage' of null
Seems like there's some magic binding that happens when executing the cast_sender.js script that I'm missing?
We've just released the Google Cast extension Beta that potentially addresses this issue. See my post: https://plus.google.com/+ShawnShen/posts/aVXSHyceNbR
You may add something like the following in your app to do both sync/async script loading.
window['_onGCastApiAvailable'] = (function(loaded, errorInfo) {
if (loaded) {
this.init();
} else {
this.appendMessage_(errorInfo);
}
}).bind(this);

Resources