Chrome Extension JavaScript Program Not Working - javascript

I am learning chrome extension programming from the tutorial here .
You can find the full code for the chrome extension here.
The code snippet where I tried to remove few links:
var clean_twitter = function(){
var ugly = [];
ugly.push('.Trends module trends');
ugly.push('.flex-module');
ugly.push('.MomentMakerHomeModule-header');
ugly.push('.Footer module roaming-module');
ugly.push('.flex-module-header');
$('.promoted-tweet').hide(); // oops! :P
for(var i=0;i<ugly.length;i++) {
var u = $(ugly[i]).find('a'); // also 'b'
u.text('');
}
}
The code tries to remove some buttons and div from the twitter website.
Now, when I put it on my pc nothing happens. I tried to remove the change link inside the trends box and it isn't removed.
Please help if I am doing something wrong here. Thanks.

At the beginning of the process_new_tweets function there's a comment explaining how the presence or absence of .mini-profile in the DOM is used as a flag.
In summary, the absence of the .mini-profile element in the DOM means that the function returns and won't proceed any further. Since the tutorial was written it would appear that Twitter no longer has a .mini-profile element anywhere in its DOM, so the function is always returning and script execution is not proceeding any further.
Remove the following lines from the beginning of the process_new_tweets function:
var mp = document.getElementsByClassName('mini-profile');
if(mp.length === 0) { return; }
And the elements that you've selected in your clean_twitter function will be removed from the DOM as expected.

Related

Chrome extension to replace Twitter words

I'm trying to make a code extension that will replace one word with another on Twitter. There's code shared on GitHubfrom 2016 that says it should work, but is having no effect when I apply the extension in my browser. Can anyone help with the issue here? Has Twitter changed too much since this code was written for it to currently work, or is there another issue? When I upload the unpacked extension into Chrome it isn't detecting any errors, but I'm not getting any results.
(function() {
function replaceTwitterWord() {
var tweetContent = document.querySelectorAll(".tweet-text");
[].slice.call(tweetContent).forEach(function(el){
var newContent = el.innerHTML.replace(/OLD_WORD_NO_QUOTATION_MARKS/g,"NEW_WORD_IN_QUOTATION MARKS");
if (newContent != el.innerHTML) {
el.innerHTML = newContent;
}
});
}
function tick() {
replaceTwitterWord();
window.setTimeout(tick, 5000);
}
tick();
})();
Well, it seems like Twitter has changed plenty of things since then. At least there is no more elements having class '.tweet-text' so you have to get another tool.

document.getElementById always returns "null" for ribbons

I need to set the background color of one of the buttons in the form's ribbon. This isn't supported through Ribbon Workbench, so I have written following javascripts to achieve the same:
function setOpportunityRibbonsAppearance() {
var submitToForeCastButton = parent.document.getElementById("opportunity|NoRelationship|Form|sfw.opportunity.Button1.Button");
if (submitToForeCastButton != null) {
submitToForeCastButton.style.backgroundColor = "lightyellow";
}
}
I have registered this scripts in Form Load event. However the issue is that, I always get parent.document.getElementById as null only.
Surprisingly, I am able to see the control while running the parent.document.getElementById statement in the browser's console, and can also change the styling attributes.
Can anyone please suggest what could be wrong here?
P.S. - I understand document.getElementById is not recommended to use in CRM, however, I am left with no other choice while trying to change the appearance of some of the buttons.
Any help on this, will be much appreciated.
You could upload an icon with a yellow background, to keep everything supported. You won't see text on yellow but it might work for you. Easy and standard.
To keep it unsupported and ugly, you could just keep on trying until you make it, setInterval allows for a function to be repeated:
function setOpportunityRibbonsAppearance() {
var submitToForeCastButton = null;
var interval = setInterval(function(){
submitToForeCastButton = parent.document.getElementById("opportunity|NoRelationship|Form|sfw.opportunity.Button1.Button");
if(submitToForeCastButton != null) {
submitToForeCastButton.style.backgroundColor = "lightyellow";
clearInterval(interval);
}
}, 500); // Every 500ms. Adjust as needed, not too fast or browser will choke.
}
Its probably because your script is running before the page is fully loaded.
Try adding a delay to the to the function Put a Delay in Javascript

Automation script is not working?

This is the first time I get my hands on with automation instruments in xcode The script works well for all button taps but the one making server connection. I don't know the reason
Here is the script I tried so far
var target = UIATarget.localTarget();
target.pushTimeout(4);
target.popTimeout();
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
appScroll.logElementTree();
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
The above script works up to showing the UIActivityIndicator instead of moving to next controller after success
I know There must be a very simple point I am missing. So help me out
UIAutomation attempts to make things "easy" for the developer, but in doing so it can make things very confusing. It sounds like you're getting a reference to window, waiting for a button to appear, then executing .tap() on that button.
I see that you've already considered messing with target.pushTimeout(), which is related to your issue. The timeout system lets you do something that would be impossible in any sane system: get a reference to an element before it exists. I suspect that behind-the-scenes, UIAutomation repeatedly attempts to get the reference you want -- as long as the timeout will allow.
So, in the example you've posted, it's possible for this "feature" to actually hurt you.
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
What if the view changes during the 2-second delay? Your reference to target.frontMostApp().mainWindow.scrollViews()[0] may be invalid, or it may not point to the object you think you're pointing at.
We got around this in our Illuminator framework by forgetting about the timeout system altogether, and just manually re-evaluating a given reference until it actually returns something. We called it waitForChildExistence, but the functionality is basically as follows:
var myTimeout = 3; // how long we want to wait
// this function selects an element
// relative to a parent element (target) that we will pass in
var selectorFn = function (myTarget) {
var ret = myTarget.frontMostApp().mainWindow.scrollViews()[0];
// assert that ret exists, is visible, etc
return ret;
}
// re-evaluate our selector until we get something
var element = null;
var later = get_current_time() + myTimeout;
while (element === null && get_current_time() < later) {
try {
element = selectorFn(target);
} catch (e) {
// must not have worked
}
}
// check whether element is still null
// do something with element
For cases where there is a temporary progress dialog, this code will simply wait for it to disappear before successfully returning the element you want.

Accessing document.activeElement.value from an extension

I'm still working on my first Chrome extension and I've hit a wall. I'm trying to insert some text into a text field using document.activeElement.value.
I can't get it to work at all. I've used tons of examples from a bunch of folks and haven't gotten it to work.
Here's my code in my background.js that the extension uses to create the context menu and insert the text. I'm assuming at this point something is wrong with my onClickHandler or click event but I'm at a loss.
chrome.runtime.onInstalled.addListener(function() {
var context = "all";
var title = "Test";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"id": "context" + context});
});
// add click event
chrome.contextMenus.onClicked.addListener(onClickHandler);
//The onClicked callback function.
function onClickHandler() {
document.activeElement.value = 'some text';
}
Okay, you need some background.
First off, you're executing code in the background page; as such, document refers to the background page itself.
Take a moment to read the Architecture Overview (and maybe the rest of the page if you're starting out, it's very helpful).
If you read that, you should understand you need a content script. Once injected in the active tab (and the "activeTab" permission should be enough to do it from the click handler), you can access the proper document.
However, this task is more complex than you think. There was a recent discussion on the topic; see this question for details.

Java Script error - can't convert undefined to object

I get "can't convert undefined to object" error while trying to run this piece of code. I'm not a programmer and can barely code therefore my question might be quite stupid/unanswerable for what I'm deeply sorry.
Code:
if (path == F[0])
{
//go N
if (pointAy > 0)
{
if (!(PS.BeadData(pointAx, pointAy - 1) === "blocked"))
{
// Set bead to Previous State
PS.BeadColor(pointAx, pointAy, previous_bead_NPC[NPCid][2]);
PS.BeadData(pointAx, pointAy, 0);
PS.BeadGlyph(pointAx, pointAy, " ");
// Increment
pointAy -= 1;
// Place NPC
MakeNPC(pointAx, pointAy, NPC[NPCid][2], NPC[NPCid][3], NPC[NPCid][4], NPC[NPCid][5], 1);
}
}
}
Can't really tell if this is enough to find an answer for you - I can post more of the code if it would help.
Maybe there is some generic answer to such an error a normal programmer would know, but such a noob like me will be oblivious to?
UPDATE
Ok, through step-by-step execution I found out that the error pops out in a different function even though disabling above piece of code makes the error not pop up. This is the function that makes the error pop up:
PS.Tick = function ()
{
"use strict";
for (var NPCid = 0; NPCid < 10; NPCid++)
{
NPCAI(NPCid);
};
};
This function is called every second and it calls AI logic function to move 10 NPC on a grid by supplying the NPCid to the AI function. Script fails here, but not always - usually one or two of the NPCs makes a step and only then the function fails.
If you are using Chrome or Firebug you can step through the JavaScript code. On Chrome, open up the tools icon (wrench in the upper right corner), then enable the developer tools by selecting "Tools" from the drop down menu and the "Developer Tools". At the bottom you should see a button in the lower part of your screen for scripts. Click on that and navigate to your page. When you see your JavaScript file open it and put a breakpoint at the "if" statement. Reload your page and then look at the values of PS and NPC when the debugger stops your code.

Categories