I have this code I want to execute everytime I watch a youtube video:
alert("Test alert");
var title = $(".style-scope ytd-video-primary-info-renderer").textContent.toUpperCase();
var blackword = "fortnite".toUpperCase();
if (title.includes(blackword)) {
alert("Video contains fortnite!");
window.location.href = "https://www.youtube.com";
}
When I visit www.youtube.com I get the test alert and nothing else happens which is fine because the script should execute only when the youtube title contains the word 'fortnite'.
I have two problems;
when I click on the video, even the test alert doesn't execute. But if I refresh the video window (with the URL something like https://www.youtube.com/watch?v=XYZ), test alert shows up, but other script doesn't execute. (When I input it manually in the console, it works..)
Does anybody seem to know what is the problem?
I am using chrome extension Custom Javascript.
Related
My manifest.json matches every URL. So, whenever, a URL is opened in Chrome, the content script is activated.
Current procedure:
(1) My background script updates the current empty URL to something.
(2) My content script is activated, because it's a match in the manifest.json. My content script needs to visit multiple URLs on the same website. It clicks on a link and navigates back (= everything in one tab) -> The content script calls itself again, because another URL is opened.
Problem:
I don't want content script to call itself multiple times. It should for example only activate, when it gets a message from the background script that a new URL was opened.
Thoughts:
chrome.windows.onCreated.addListener(function()) is around all my code in the background script. Is there something similar for the content script that i'm looking for?
Maybe something like:
window.addEventListener
browser.runtime.onMessage.addListener
browser.runtime.sendMessage
browser.runtime.onMessage
browser.tabs.sendMessage()
But i don't understand how to use them properly.
In your background script, do this:
chrome.windows.onCreated.addListener(function(tab) {
let msg = {txt: "execute"};
chrome.tabs.sendMessage(tab.id, msg);
};
And then in your content script, you can intercept this message via:
chrome.runtime.onMessage.addListener(function(msg){
if(msg.txt == "execute") {
doSomething();
}
});
Docs here.
https://developer.chrome.com/extensions/runtime#method-sendMessage
https://developer.chrome.com/extensions/runtime#event-onMessage
I am trying to get some information from innerHTML from Youtube source page.
However, when I do this
var myStr = document.getElementById('player').innerHTML;
alert(myStr);
this only procs the alert if I visit the same Youtube page twice. If I navigate to a different video, no alert shows up, unless I refresh the page again.
I am also doing this:
if(document.readyState === 'loading') {
document.addEventListener("load", searchSomething, false);
} else {
searchSomething();
}
at the beginning of the Javscript just to make sure that I am getting the innerHTML after the whole page loads. What is the problem here? How do I fix this so that I do not have to refresh the page to proc the alert!
UPDATE ----------------------
I started to log every event that gets fired on Youtube.
However, Youtube does not fire any events including "load", "unload", "hashchange". It only fires Javascript events if you refresh the page!!!
How can this be possible?
If you use jquery it will be like this
$(document).ready(function() {
var myStr = document.getElementById('player').innerHTML;
alert(myStr);
}
I am trying to send data to a popup window which opens by clicking a button.
All my code is in php where I include javascript and html, I will just show html and js here.
Consider my popup window (say, popupwindow.html) code as just
<div id = 'getData'></div>
Now on the page(say, main.js) where I put the button clicking which the popup opens, I am computing some data and trying to send it to popupwindow.html.
Here is relevant main.js code that gets executed after clicking the button
var popup = window.open("popupwindow.html", "popup", extraParams); //ignore extraParams
$(popup).on('load', function() {
//console.log("inside onload function");
popup.document.getElementById("getData").innerHTML = data; //here data is some string
});
Now this code works perfectly on Chrome and Firefox, but in IE load() function does not get executed. I know this because the console.log line when uncommented, does not print on the console. Tried versions of jQuery from 1.2.3 to 2.2.1 where the code runs successfully on Chrome and Firefox(but not in IE)
Note: I cannot execute the required code on popup page(popupwindow.html) because of some constraints. I have to write it on main.js and send it to popup.
I saw all other questions which said onload() or similar function in IE gives problems but I still could not find the appropriate solution. Please let me know how I can fix this for IE.
So using TestComplete I'm essentially trying to open up a session on chrome, navigate to a web page, and then click a button on that page. after I'm finished I want to close that browser page. I'm having trouble closing the page though. Here is the code I have so far.
function ChromeTest
{
Browsers.Item(btChrome).Run(MyWebAdress);
var browser = Sys.Browser("chrome");
var page = Sys.Browser("chrome").Page(MyWebAdress);
var MyButton = page.ButtonLocation;
MyButton.click();
browser.BrowserWindow.Close(5000);
}
however, at the Close line I get an error that says "Unable to find the object BrowserWindow". Thanks in advance for any help you have.
Change BrowserWindow to BrowserWindow(0) (or whatever index you see in the Object Browser):
browser.BrowserWindow(0).Close(5000);
Or you can call Close() directly on the Chrome process:
browser.Close(5000);
I would like to write a html page where clicking on the hyperlink can be traced. That means I can get a statistic about how many people click on a hyperlink in my page, in a past period. If it is possible to know their ID, it would be better.
And I find this piece of code from the source of a web page:
<script type="text/javascript">
function stc(e,linkIndex) {
if (document.images) {
var linkText;
if (navigator.appName.toLowerCase()=="microsoft internet explorer") {
linkText=e.innerText}
else {
linkText=e.textContent}
if (linkText=="") {
if (e.firstChild) {
var firstChild=e.firstChild.nodeName.toUpperCase();
if (firstChild=="IMG") {
linkText="Image: "+getName(e.firstChild.getAttribute('src'))}}
else {
var nodeName=e.nodeName.toUpperCase();
if (nodeName=="AREA") {
linkText="ImageMap: "+e.href}}}
if (linkText=="") {
linkText=e.href}
(new Image()).src="/a/i/stg.gif?f="+escape(document.location.href)+"&t="+escape(e.href)+"&i="+linkIndex+"&n="+escape(trimString(linkText))}}
function getName(s) {
if (s.lastIndexOf('/')>=0) {
return(s.substring(s.lastIndexOf('/')+1,s.length))}
else {
return(s)}}
function trimString(s) {
return(s.replace(/^\s*/,"").replace(/\s*$/,""))}
</script>
and I guess google will be able to track information of clinking on this link.
I don't know too much about Javascript, could anyone tell me, according to this code, where the tracing information is saved?
usually trace() in other languages is is just some kind of output and the best most simple way to do that in javascript is to call console.log("some output "). you can view the output in Google Chrome by right click on the page > inspect element, then click on the console tab, there you will see your output. in Firefox, you should get the Firebug add-on, there you can see the same output generated by console.log("some output") again within the console tab.
other browsers suck for dev so why even bother explaining?
You need serverside code to do this.
For example, you could have a gateway script that redirects users to the page they want to see:
http://www.example.com/portal/www.google.com/
From there, you can just save the user's request into a database and redirect the user to www.google.com almost instantly without the user really caring.